Definition of "property"

Eryk Sun eryksun at gmail.com
Tue Jun 1 15:13:57 EDT 2021


On 6/1/21, Jon Ribbens via Python-list <python-list at python.org> wrote:
>
> I already answered that in the post you are responding to, but you
> snipped it: You can tell something's definitely not a data attribute
> if you have to put brackets after its name to call it as a method to
> invoke its function or retrieve the value it returns.

I prefer to use the generic term "computed attribute", which doesn't
interfere with the use of "data" in Python's concept of a data
descriptor and instance data. All descriptors are accessed without
calling them. Usually a non-data descriptor returns a bound callable
object, such as a method. But it can return anything. For example,
take the following P descriptor type:

    class P:
       def __get__(self, obj, cls):
           if obj is not None:
               return 42
           return self

    class C:
       p = P()

    obj = C()

    >>> obj.p
    42

The P type doesn't implement __set__ or __delete__, so it's not a data
descriptor. This means we can set instance data `p` that overrides the
computed attribute. For example:

    >>> obj.p = 21
    >>> vars(obj)
    {'p': 21}
    >>> obj.p
    21

    >>> del obj.p
    >>> obj.p
    42


More information about the Python-list mailing list