Questions on Instance methods

Arup Rakshit ar at zeit.io
Fri Apr 19 09:37:10 EDT 2019


>    When an instance method object is created by retrieving a class method object from a class or instance, its __self__ attribute is the class itself, and its __func__ attribute is the function object underlying the class method.

Here I have 2 questions:

1. How do you create an instance method object from a class method object by using either the class or the instance?
2. Why in both cases the __self__ is set to Class only?
3. Would you give me examples also while explaining this?

>   When an instance method object is derived from a class method object, the “class instance” stored in __self__ will actually be the class itself, so that calling either x.f(1) or C.f(1) is equivalent to calling f(C,1) where f is the underlying function.

Here x is an instance of C. Would you give me an example to illustrate why " x.f(1) or C.f(1) is equivalent to calling f(C,1)” ?

Quotes are taken from https://docs.python.org/3/reference/datamodel.html#the-standard-type-hierarchy under Callable types -> User-defined functions / Instance methods

Thanks,

Arup Rakshit
ar at zeit.io





More information about the Python-list mailing list