P
P
ProgramDevel2021-11-08 16:33:02
Python
ProgramDevel, 2021-11-08 16:33:02

Is it true that a class attribute overrides an object attribute?

Is it true that a class attribute overrides an object attribute?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2021-11-08
@Vindicar

Take and check

>>> class C:
...   x = 42
...   def __init__(self):
...     self.x = 13
...
>>> i = C()
>>> i.x
13
>>> C.x
42
>>>

Which is logical - the object first looks for attributes in its __dict__, then in the __dict__ of the class, then it goes through the class's ancestors.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question