[Python-ideas] Specificity in AttributeError

Eric Snow ericsnowcurrently at gmail.com
Sat Apr 27 20:04:44 CEST 2013


On Apr 26, 2013 10:45 PM, "Devin Jeanpierre" <jeanpierreda at gmail.com> wrote:
>
> Code:
>      class A:
>         @property
>         def text(self):
>             return self.foo
>
> Behavior:
>     >>> hasattr(A(), 'text')
>     False
>
> Actually, this is absolutely correct. "A().y" does not give a result
> -- in fact, it raises AttributeError. Behavior wise, this is exactly
> what it means for an attribute to not exist.
>

What about using inspect.getattr_static()?

def hasattr_static(obj, name):
    try:
        getattr_static(obj, name)
    except AttributeError:
        return False
    else:
        return True

-eric
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130427/8e42e639/attachment.html>


More information about the Python-ideas mailing list