[Python-Dev] 'hasattr' is broken by design
Benjamin Peterson
benjamin at python.org
Mon Aug 23 22:45:58 CEST 2010
2010/8/23 Raymond Hettinger <raymond.hettinger at gmail.com>:
> I don't have a specific proposal in mind. My main questions are
> * Is there anything that hasattr(obj, key) can or should do that
> can't already be done with getattr(obj, key, None)?
> If not, do we really need to change anything?
For one, it's annoying when "key" can be None.
> * Why do people typically use hasattr() instead getattr()?
> Aren't they are really trying to just determine
> whether a key exists somewhere in the MRO?
> If so, then doing anything more than that is probably a surprise.
It's generally more convenient that getattr(obj, "blah", None) is not None.
>
> P.S. The current behavior seems to be deeply embedded:
Well, that's what happens when a behavior is added in 1990. :)
--
Regards,
Benjamin
More information about the Python-Dev
mailing list