The right way to 'call' a class attribute inside the same class
Juan C.
juan0christian at gmail.com
Mon Dec 12 11:38:24 EST 2016
On Sun, Dec 11, 2016 at 11:34 PM, Steve D'Aprano
<steve+python at pearwood.info> wrote:
> So... in summary:
>
>
> When *assigning* to an attribute:
>
> - use `self.attribute = ...` when you want an instance attribute;
>
> - use `Class.attribute = ...` when you want a class attribute in
> the same class regardless of which subclass is being used;
>
> - use `type(self).attribute = ...` when you want a class attribute
> in a subclass-friendly way.
>
>
> When *retrieving* an attribute:
>
> - use `self.attribute` when you want to use the normal inheritance
> rules are get the instance attribute if it exists, otherwise a
> class or superclass attribute;
>
> - use `type(self).attribute` when you want to skip the instance
> and always return the class or superclass attribute.
Thanks, that seems simple enough.
More information about the Python-list
mailing list