python / cpython

The Python programming language
https://www.python.org
Other
62.29k stars 29.93k forks source link

Simpler implementation of `Property` class in pure Python #123176

Closed dongbohu closed 3 weeks ago

dongbohu commented 3 weeks ago

Documentation

The Descriptor Guide Howto implements a sample Property class that is equivalent to property builtin class: https://github.com/python/cpython/blob/bffed80230f2617de2ee02bd4bdded1024234dab/Doc/howto/descriptor.rst?plain=1#L1046

I wonder whether the getter, setter and deleter methods can be simplified like this:

class Property:
    # ...
    def getter(self, fget):
        self.fget = fget
        return self

    def setter(self, fset):
        self.fset = fset
        return self

    def deleter(self, fdel):
        self.fdel = fdel
        return self

Thanks.

picnixz commented 3 weeks ago

The docs explicitly states:

https://github.com/python/cpython/blob/bffed80230f2617de2ee02bd4bdded1024234dab/Doc/howto/descriptor.rst?plain=1#L992-L993

In particular, the purpose is not to have a simple example but to have a pure Python equivalent matching the C implementation (see https://github.com/python/cpython/blob/bffed80230f2617de2ee02bd4bdded1024234dab/Objects/descrobject.c#L1761 and https://github.com/python/cpython/blob/bffed80230f2617de2ee02bd4bdded1024234dab/Objects/descrobject.c#L1572 to understand what happens in C for property.getter)

Closing as wontfix.