3

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 example which handles any property requested which is not defined explicitly elsewhere in the class...

class Props:
    def __getattr__(self, attr):
        return 'some_new_value'

>>> p = Props()
>>> p.prop                          # Property get handled
'some_new_value'

>>> p.func('an_arg', kw='keyword')  # Function call NOT handled
Traceback (most recent call last):
  File "<console>", line 1, in <module>
TypeError: 'str' object is not callable

Next, consider this example which handles any function call not defined explicitly elsewhere in the class...

class Funcs:
    def __getattr__(self, attr):
        def fn(*args, **kwargs):
            # Do something with the function name and any passed arguments or keywords
            print attr
            print args
            print kwargs
            return 
        return fn

>>> f = Funcs()
>>> f.prop                          # Property get NOT handled
<function fn at 0x10df23b90>

>>> f.func('an_arg', kw='keyword')  # Function call handled
func
('an_arg',)
{'kw': 'keyword'}

The question is how to handle both types of missing attributes in the same __getattr__? 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? Essentially I want to handle SOME missing property attributes AND SOME missing function attributes and then resort to default behavior for all the other cases.

Advice?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
sansjoe
  • 262
  • 3
  • 14
  • 3
    Methods are attributes too; attributes are *always* either a callable (e.g. a method) or some other non-callable type; they are never both at the same time. What are you trying to achieve? – Martijn Pieters Jan 30 '13 at 19:53
  • "I want to handle SOME missing property attributes AND SOME missing function attributes and then resort to default behavior for all the other cases." – sansjoe Jan 30 '13 at 19:55
  • Or just as good, return attribute not found for property requests, but handle function requests. – sansjoe Jan 30 '13 at 19:56
  • 1
    What would you expect to happen if I did `print(magicthing.func)`? Should it print `some_new_value` or ``? – abarnert Jan 30 '13 at 20:02
  • Also, what's the actual use case here? This seems like an [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem), where the real problem is most likely in the design of the code that you want to use this thing from. – abarnert Jan 30 '13 at 20:02
  • Thanks for posting the real answer here which is "it's not possible". But it took the typical amount of smugness that is typical "Since I the amazing developer can't imagine a reason why this would be valuable, you must be doing something wrong and don't actually need an answer to your question." Thanks to the helpful, factual responses sans design consultancy. – sansjoe Feb 01 '13 at 01:22
  • @sansjoe: You're clearly smart enough to know you're going to get these responses, deserved or not. So you should be smart enough to figure out how to head them off. All you have to do is explain why you want to do this, in the question. That will inspire creative thinking instead of "no, you don't want to do this" answers. (Not 100%, but a _lot_ better than if you don't explain.) If you can't explain why you need it, at the very least acknowledge that you know it's a non-idiomatic, code-smelly, usually-wrong thing to do. – abarnert Feb 01 '13 at 01:48
  • If the question is explained with sufficient context, which it is, the ultimate objective of the questioner need not concern the answerer. Now a piece of advice, when you are asked a question, do your best to answer it without gracing the hapless inquisitor with a speech and/or extraneous opinions. Nevertheless, I do appreciate the actual answer and insight you provided below, which was indeed very helpful and what I needed. – sansjoe Feb 02 '13 at 03:08

2 Answers2

3

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
abarnert
  • 354,177
  • 51
  • 601
  • 671
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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343