The right way to 'call' a class attribute inside the same class

Thomas 'PointedEars' Lahn PointedEars at web.de
Mon Dec 12 09:44:25 EST 2016


Thomas 'PointedEars' Lahn wrote:

> Note that (AIUI) in this example the instances of the class referred by
> “C” do not have an *own* “foo” property in the beginning, so until bar()
> is called on them, they inherit that property (and its value) from that
> class.³

For proper *Python* terminology, s/property/attribute/g here (see below).

> […]
> ________
> […]
> ³  How can one tell the difference in Python between a pre-initialized,
>    inherited attribute value and one own that is just equal to the
>    inherited one?  In ECMAScript, this.hasOwnProperty("foo") would return
>    “false” if the property were inherited, “true” otherwise.  But in
>    Python, hasattr(self, "foo") returns “True” regardless whether the
>    “foo” attribute of the calling instance has been assigned a value
>    explicitly.  What is the Python equivalent of ECMAScript’s
>    Object.prototype.hasOwnProperty() method?

-- 
PointedEars

Twitter: @PointedEars2
Please do not cc me. / Bitte keine Kopien per E-Mail.


More information about the Python-list mailing list