Markus Meskanen Markus Meskanen - 1 year ago 79
Python Question

Why do people default owner parameter to None in __get__?

I've seen this quite often:

def __get__(self, instance, owner=None):

Why do some people use the default value of
for the the

This is even done in the Python docs:

descr.__get__(self, obj, type=None) --> value

Answer Source

Because the owner can easily be derived from the instance, the second argument is optional. Only when there is no instance to derive an owner from, is the owner argument needed.

This is described in the proposal that introduced descriptors, PEP 252 - Making Types Look More Like Classes:

__get__: a function callable with one or two arguments that retrieves the attribute value from an object. This is also referred to as a "binding" operation, because it may return a "bound method" object in the case of method descriptors. The first argument, X, is the object from which the attribute must be retrieved or to which it must be bound. When X is None, the optional second argument, T, should be meta-object and the binding operation may return an unbound method restricted to instances of T.

(Bold emphasis mine).

Binding, from day one, was meant to be applicable to the instance alone, with the type being optional. Methods don't need it, for example, since they can be bound to the instance alone:

>>> class Foo: pass
>>> def bar(self): return self
>>> foo = Foo()
>>> = bar.__get__(foo)  # look ma! no class!
<bound method of <__main__.Foo object at 0x10a0c2710>>
<__main__.Foo object at 0x10a0c2710>

Besides, the second argument can easily be derived from the first argument; witness a classmethod still binding to the class even though we did not pass one in:

>>> classmethod(bar).__get__(foo)
<bound method of <class '__main__.Foo'>>
>>> classmethod(bar).__get__(foo)()
<class '__main__.Foo'>

The only reason the argument is there in the first place is to support binding to class, e.g. when there is no instance to bind to. The class method again; binding to None as the instance won't work, it only works if we actually pass in the class:

>>> classmethod(bar).__get__(None)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: __get__(None, None) is invalid
>>> classmethod(bar).__get__(None, Foo)
<bound method of <class '__main__.Foo'>>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download