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

Chris Angelico rosuav at gmail.com
Mon Dec 12 11:17:25 EST 2016


On Tue, Dec 13, 2016 at 1:34 AM, Thomas 'PointedEars' Lahn
<PointedEars at web.de> wrote:
> ³  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?

You could check "foo" in self.__dict__, but I don't know of any
real-world situations where you need to.

ChrisA


More information about the Python-list mailing list