[Python] Determine if __getattr__ is method or attribute call

Discussão em 'Python' iniciado por Stack, Setembro 24, 2024.

  1. Stack

    Stack Membro Participativo

    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.

    Continue reading...

Compartilhe esta Página