[Zope-dev] "hasattr" geddon
Dieter Maurer
dieter at handshake.de
Fri Jul 9 10:22:17 EDT 2004
Yesterday, I upgraded to Zope 2.7.1.
Because several of our extensions broke, I have to look into detail
at the changed and I saw that the "hasattr geddon" was started.
Unfortunately, in a way that I feel as not optimal and error prone...
In the module I saw, "if hasattr(obj, attr)" has been replaced by
"if getattr(obj, attr, None)".
This is neither general (in the special case, it was at least not false),
nor optimal (in the special case, the boolean check was very expensive)
nor clear.
I propose to implement the "hasattr geddon" instead by a monkey
patch for Python's builtin "hasattr".
This has the following advantages:
* no code changes needed with the exception of the monkey patch
installation
* the code is as clear as possible
* the implementation can be made efficient and safe
* no danger of missed "hasattr" occurrences
Disadvantages:
* Occasionally, a "hasattr" call might become slightly more
inefficient than necessary (when "hasattr" operates
on an object which is not persistent).
* the usual problems with monkey patches (if someone else tries
to monkey patch "hasattr", too)
The "hasattr" replacement in Python's "__builtin__" could look like:
_marker = []
def hasattr(obj, attr):
return getattr(obj, attr, _marker) is not _marker
Opinions?
--
Dieter
More information about the Zope-Dev
mailing list