[Zope-dev] Re: hasattr implementation for Zope?
Paul Winkler
pw_lists at slinkp.com
Fri May 27 13:41:03 EDT 2005
On Fri, May 27, 2005 at 01:10:29PM -0400, Jim Fulton wrote:
> Paul Winkler wrote:
> >On Fri, May 27, 2005 at 12:08:55PM -0400, Paul Winkler wrote:
> >
> >>On Fri, May 27, 2005 at 11:49:03AM -0400, Jim Fulton wrote:
> >>
> >>>BTW, I prefer to code things like this in the following way:
> >>>
> >>> def safe_hasattr():
> >>> marker = object()
> >>>
> >>> def safe_hasattr(obj, attr):
> >>> return getattr(obj, attr, marker) is not marker
> >>>
> >>> return safe_hasattr
> >>>
> >>> safe_hasattr = safe_hasattr()
> >>>
> >>>This way, there aren't any extra arguments or funny
> >>>globals to confuse matters. (I've seen code that broke
> >>>because people tried to reuse marker definitions accross
> >>>modules.
> >
> >
> >One more thought... as for "funny globals", it seems to me that a
> >"private" global like __marker = [] results in cleaner code than either
> >your approach or the keyword arg hack.
>
> I don't agree. In my approach, there isn't a global to begin with.
Yeah, but to me, it seems like a nested function definition
and rebinding the name is a bit much bother just to avoid a single
private global. It adds a bit of mental effort for readers who haven't
seen the idiom before, especially newcomers to python.
> Note that in my version above, marker is a local rather than a global
> and gets looked up at local-variable speed.
Yes, that is very nice.
--
Paul Winkler
http://www.slinkp.com
More information about the Zope-Dev
mailing list