[Python-ideas] Multiple arguments for decorators

Nick Coghlan ncoghlan at gmail.com
Tue Dec 1 23:29:54 EST 2015


On 2 December 2015 at 01:29, Random832 <random832 at fastmail.com> wrote:
> Incidentally, I'm not sure I understand what the owner parameter is for,
> and what purpose it can be useful for which it isn't also needed on the
> setter.

__get__ is the only case where the descriptor can override retrieval
via the *class* in addition to via the instance. That case shows up as
the instance being "None" in the call to __get__:

>>> class ShowDescr:
...     def __get__(self, instance, owner):
...         print(self, instance, owner, sep="\n")
...
>>> class C:
...     x = ShowDescr()
...
>>> C.x
<__main__.ShowDescr object at 0x7f7ddcf3f400>
None
<class '__main__.C'>
>>> C().x
<__main__.ShowDescr object at 0x7f7ddcf3f400>
<__main__.C object at 0x7f7ddcf3f4a8>
<class '__main__.C'>

For __set__ and __del__, setting and deleting via the class isn't
something a descriptor stored on that class can affect (it needs to be
on the metaclass instead)

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-ideas mailing list