Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the usage of third argument objtype in Python descriptor's __get__ [duplicate]

I know that in Python we have to supply __get__ function when implementing a descriptor. The interface is like:

def __get__(self, obj, objtype=None):
    pass

My question is:

Why we have to supply objtype arg? What is objtype used for?

I did not see some examples about the usage of this arg.

like image 893
ruanhao Avatar asked May 25 '15 08:05

ruanhao


People also ask

What is __ get __ in Python?

Python's __get__() magic method defines the dynamic return value when accessing a specific instance and class attribute. It is defined in the attribute's class and not in the class holding the attribute (= the owner class).

What is the use of descriptor in Python?

Python descriptors are a way to create managed attributes. Among their many advantages, managed attributes are used to protect an attribute from changes or to automatically update the values of a dependant attribute. Descriptors increase an understanding of Python, and improve coding skills.

What is descriptor object in Python?

Descriptors are Python objects that implement a method of the descriptor protocol, which gives you the ability to create objects that have special behavior when they're accessed as attributes of other objects.

What is descriptor What is the purpose of descriptor?

A descriptor is a mechanism behind properties, methods, static methods, class methods, and super() . Descriptor protocol : In other programming languages, descriptors are referred to as setter and getter, where public functions are used to Get and Set a private variable.


1 Answers

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 Noneand 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>
like image 120
Ashwini Chaudhary Avatar answered Oct 07 '22 18:10

Ashwini Chaudhary