Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are arguments passed to a function through __getattr__

Consider the following code example (python 2.7):

class Parent:
    def __init__(self, child):
        self.child = child

    def __getattr__(self, attr):
        print("Calling __getattr__: "+attr)
        if hasattr(self.child, attr):
            return getattr(self.child, attr)
        else:
            raise AttributeError(attr)

class Child:
    def make_statement(self, age=10):
        print("I am an instance of Child with age "+str(age))

kid = Child()
person = Parent(kid)

kid.make_statement(5)
person.make_statement(20)

it can be shown, that the function call person.make_statement(20) calls the Child.make_statement function through the Parent's __getattr__ function. In the __getattr__ function I can print out the attribute, before the corresponding function in the child instance is called. So far so clear.

But how is the argument of the call person.make_statement(20) passed through __getattr__? How am I able to print out the number '20' in my __getattr__ function?

like image 289
Alex Avatar asked Dec 08 '12 10:12

Alex


People also ask

What is Getattr () used for?

Python getattr() is a built-in function that is used to return the value of an attribute of a specific object/instance. The Python getattr() function is used to obtain an object's attribute value and also provides the option of executing the default value if the attribute is not available.

What is __ Getattr __ in Python?

__getattr__Called when an attribute lookup has not found the attribute in the usual places (i.e. it is not an instance attribute nor is it found in the class tree for self).

What does Getattr return?

The getattr() method returns the value of the named attribute of an object. If not found, it returns the default value provided to the function.


1 Answers

You are not printing 20 in your __getattr__ function. The function finds the make_statement attribute on the Child instance and returns that. As it happens, that attribute is a method, so it is callable. Python thus calls the returned method, and that method then prints 20.

If you were to remove the () call, it would still work; we can store the method and call it separately to get 20 printed:

>>> person.make_statement
Calling __getattr__: make_statement
<bound method Child.make_statement of <__main__.Child instance at 0x10db5ed88>>
>>> ms = person.make_statement
Calling __getattr__: make_statement
>>> ms()
I am an instance of Child with age 10

If you have to see the arguments, you'd have to return a wrapper function instead:

def __getattr__(self, attr):
    print("Calling __getattr__: "+attr)
    if hasattr(self.child, attr):
        def wrapper(*args, **kw):
            print('called with %r and %r' % (args, kw))
            return getattr(self.child, attr)(*args, **kw)
        return wrapper
    raise AttributeError(attr)

This now results in:

>>> person.make_statement(20)
Calling __getattr__: make_statement
called with (20,) and {}
I am an instance of Child with age 20
like image 181
Martijn Pieters Avatar answered Oct 02 '22 11:10

Martijn Pieters