Skip to content Skip to sidebar Skip to footer

How To Handle & Return Both Properties AND Functions Missing In A Python Class Using The __getattr__ Function?

It is fairly easy to use the __getattr__ special method on Python classes to handle either missing properties or functions, but seemingly not both at the same time. Consider this e

Solution 1:

How to detect if the attribute requested was in property notation or in method notation with parentheses and return either a value or a function respectively?

You can't. You also can't tell whether a requested method is an instance, class, or static method, etc. All you can tell is that someone is trying to retrieve an attribute for read access. Nothing else is passed into the getattribute machinery, so nothing else is available to your code.

So, you need some out-of-band way to know whether to create a function or some other kind of value. This is actually pretty common—you may actually be proxying for some other object that does have a value/function distinction (think of ctypes or PyObjC), or you may have a naming convention, etc.

However, you could always return an object that can be used either way. For example, if your "default behavior" is to return attributes are integers, or functions that return an integer, you can return something like this:

class Integerizer(object):
    def __init__(self, value):
        self.value = value
    def __int__(self):
        return self.value
    def __call__(self, *args, **kw):
        return self.value

Solution 2:

There is no way to detect how the returned attribute was intended to be used. Everything on python objects are attributes, including the methods:

>>> class Foo(object):
...     def bar(self): print 'bar called'
...     spam='eggs'
... 
>>> Foo.bar
<unbound method Foo.bar>
>>> Foo.spam
'eggs'

Python first looks up the attribute (bar or spam), and if you meant to call it (added parenthesis) then Python invokes the callable after lookup up the attribute:

>>> foo = Foo()
>>> fbar = foo.bar
>>> fbar()
'bar called'

In the above code I separated the lookup of bar from calling bar.

Since there is no distinction, you cannot detect in __getattr__ what the returned attribute will be used for.

__getattr__ is called whenever normal attribute access fails; in the following example monty is defined on the class, so __getattr__ is not called; it is only called for bar.eric and bar.john:

>>> class Bar(object):
...     monty = 'python'
...     def __getattr__(self, name):
...         print 'Attribute access for {0}'.format(name)
...         if name == 'eric':
...             return 'idle'
...         raise AttributeError(name)
... 
>>> bar = Bar()
>>> bar.monty
'python'
>>> bar.eric
Attribute access for eric
'idle'
>>> bar.john
Attribute access for john
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 7, in __getattr__
AttributeError: john

Note that functions are not the only objects that you can invoke (call); any custom class that implements the __call__ method will do:

>>> class Baz(object):
...    def __call__(self, name):
...        print 'Baz sez: "Hello {0}!"'.format(name)
...
>>> baz = Baz()
>>> baz('John Cleese')
Baz sez: "Hello John Cleese!"

You could use that return objects from __getattr__ that can both be called and used as a value in different contexts.


Post a Comment for "How To Handle & Return Both Properties AND Functions Missing In A Python Class Using The __getattr__ Function?"