Determine if __getattr__ is method or attribute call

为君一笑 提交于 2019-11-28 00:25:20

问题


Is there any way to determine the difference between a method and an attribute call using __getattr__?

I.e. in:

class Bar(object):
    def __getattr__(self, name):
        if THIS_IS_A_METHOD_CALL:
            # Handle method call
            def method(**kwargs):
                return 'foo'
            return method
        else:
            # Handle attribute call
            return 'bar'

foo=Bar()
print(foo.test_method()) # foo
print(foo.test_attribute) # bar

The methods are not local so it's not possible to determine it using getattr/callable. I also understand that methods are attributes, and that there might not be a solution. Just hoping there is one.


回答1:


You cannot tell how an object is going to used in the __getattr__ hook, at all. You can access methods without calling them, store them in a variable, and later call them, for example.

Return an object with a __call__ method, it'll be invoked when called:

class CallableValue(object):
    def __init__(self, name):
        self.name = name
    def __call__(self, *args, **kwargs):
        print "Lo, {} was called!".format(self.name)

class Bar(object):
    def __getattr__(self, name):
        return CallableValue(name)

but instances of this will not be the same thing as a string or a list at the same time.

Demo:

>>> class CallableValue(object):
...     def __init__(self, name):
...         self.name = name
...     def __call__(self, *args, **kwargs):
...         print "Lo, {} was called!".format(self.name)
... 
>>> class Bar(object):
...     def __getattr__(self, name):
...         return CallableValue(name)
... 
>>> b = Bar()
>>> something = b.test_method
>>> something
<__main__.CallableValue object at 0x10ac3c290>
>>> something()
Lo, test_method was called!



回答2:


In short, no, there is no reliable way - the issue is that a method is an attribute in Python - there is no distinction made. It just happens to be an attribute that is a bound method.

You can check if the attribute is a method, but there is no guarantee that means it will be called, e.g:

class Test:
    def test(self):
        ...

Test().test  # This accesses the method, but doesn't call it!

There is no way for the call accessing the function to know if it's going to be called when it is returned - that's a future event that hasn't yet been processed.

If you are willing to assume that a method being accessed is a method being called, you can determine that it is a method being accessed with a check like this:

hasattr(value, "__self__") and value.__self__ is self

Where value is the attribute you want to check to see if it is a method or some other attribute, and self is the instance you want to see if it's a method for.

If you need something to happen when it is called, you could use this moment to decorate the function.

An solid code example of this can be found here.



来源:https://stackoverflow.com/questions/20120983/determine-if-getattr-is-method-or-attribute-call

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!