It gives users an option to do something with the class that was used to call the descriptor.
In normal cases when the descriptor is called through the instance we can get the object type by calling type(ins)
.
But when it is called through the class ins
will be None
and we won't be able to access the class object if the third argument was not present.
Take functions in Python for example, each function is an instance of types.FunctionType
and has a __get__
method that can be used to make that function a bound or unbound method.
>>> from types import FunctionType
>>> class A(object):
pass
...
>>> def func(self):
print self
...
>>> ins = A()
>>> types.FunctionType.__get__(func, ins, A)() # instance passed
<__main__.A object at 0x10f07a150>
>>> types.FunctionType.__get__(func, None, A) # instance not passed
<unbound method A.func>
>>> types.FunctionType.__get__(func, None, A)()
Traceback (most recent call last):
File "<ipython-input-211-d02d994cdf6b>", line 1, in <module>
types.FunctionType.__get__(func, None, A)()
TypeError: unbound method func() must be called with A instance as first argument (got nothing instead)
>>> types.FunctionType.__get__(func, None, A)(A())
<__main__.A object at 0x10df1f6d0>
object.__get__
does not mentionobjtype
. Where did you read about it? – Acridineowner
isobjtype
in this case, i.e the class. – Zadazadackowner
(notobjtype
, as already pointed out) does not default toNone
in any version of Python. – Pend