[Python-ideas] Multiple arguments for decorators

Nick Coghlan ncoghlan at gmail.com
Tue Dec 1 05:09:03 EST 2015


On 1 December 2015 at 17:51, Sjoerd Job Postmus <sjoerdjob at sjec.nl> wrote:
> What worries me is that all we're looking at is the case of the
> @property decorator. That decorator just creates a descriptor. Why not
> just
>
> class Foo(object):
>     class myprop(object):
>         def __get__(self):
>             return 1
>         def __set__(self, value):
>             pass

Those aren't the signatures of the descriptor methods. From
https://docs.python.org/3/reference/datamodel.html#descriptors:

    object.__get__(self, instance, owner)
    object.__set__(self, instance, value)
    object.__delete__(self, instance)

The trick with property is that it hides the "self" that refers to the
descriptor object itself, as well as the "owning class" reference in
__get__, leading to the simplified property protocol where the
individual functions are written as instance methods of the class
*containing* the property, and retrieving the descriptor from the
class will just give you the descriptor object.

Cheers,
Nick.

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


More information about the Python-ideas mailing list