Why do data descriptors (e.g. properties) take priority over instance attributes?

Peter J. Holzer hjp-python at hjp.at
Thu Dec 27 08:24:12 EST 2018


On 2018-12-27 14:17:34 +0100, Peter J. Holzer wrote:
> On 2018-12-17 21:57:22 +1100, Paul Baker wrote:
> >     class C:
> >         def __init__(self):
> >             self.__dict__['a'] = 1
> > 
> >         @property
> >         def a(self):
> >             return 2
> > 
> >     o = C()
> >     print(o.a)  # Prints 2
> 
> What version of Python is this? I get a different result:
> 
>     Python 3.6.7 (default, Oct 22 2018, 11:32:17) 
>     [GCC 8.2.0] on linux
>     Type "help", "copyright", "credits" or "license" for more information.
>     >>> class C:
>     ...   def __init__(self):
>     ...     self.__dict__['a'] = 1
>     ...     @property
>     ...     def a(self):
>     ...       return 2
>     ... 
>     >>> o = C()
>     >>> o.a
>     1

Oops. I messed up the indentation. 

With the same indentation I get the same result.

        hp

-- 
   _  | Peter J. Holzer    | we build much bigger, better disasters now
|_|_) |                    | because we have much more sophisticated
| |   | hjp at hjp.at         | management tools.
__/   | http://www.hjp.at/ | -- Ross Anderson <https://www.edge.org/>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20181227/bf9fbd95/attachment.sig>


More information about the Python-list mailing list