[Python-talk] can't make meeting

Kent Johnson kent37 at tds.net
Thu May 22 17:30:37 EDT 2008


On Thu, May 22, 2008 at 5:12 PM, Lloyd Kvam <python at venix.com> wrote:
> I do have a gotcha to mention:
>
> I have some old python code that used __getattr__ to handle things like
> delegating attribute lookup to a component and computing answers for
> "synthetic" attributes.
>
> I used properties to add a new computed property and it just did not
> work.  Then I realized that I really could not mix __getattr__ based
> logic with properties.
>
> __getattr__ operates against the instance __dict__ and gets control when
> an attribute reference is not in __dict__.  Properties are handled
> through the class __dict__.  So a reference to an attribute provided by
> properties will first trigger the __getattr__ logic.

Hmm. Can you give an example? I am not seeing that. For example:

class New(object):
    def __getattr__(self, name):
        print 'getattr', name
        return name

    @property
    def bar(self):
        print 'bar called'
        return 'bar'


o = New()
print o.foo
print o.bar


prints:

getattr foo
foo
bar called
bar

so __getattr__() is not called for the access to bar.

You were using new-style classes, right? Properties don't work
correctly with old-style classes.

Kent


More information about the Python-talk mailing list