'The child class does not call code of the parent class
The child class does not call code of the parent class. I wrote this code. I thought the Id field of the Extension2 class would be 2, but it is 1
myvariable = 0
lock = threading.Lock()
def get_next_id() -> int:
global myvariable
global lock
with lock:
myvariable += 1
return myvariable
class Extension:
Id = get_next_id()
class Extension2(Extension):
pass
Solution 1:[1]
The way you have structured your class, Id is not an instance variable for the class. You probably want to use __init__ to initialize data members of a class. For example:
def set_variable():
return "id"
class Extension():
def __init__(self):
self.Id = set_variable()
class Extension2(Extension):
pass
Then when you create class instances, their members are automatically assigned, and this is true of the child class as well, which will inherit instance variables of the parent unless you specify otherwise.
r = Extension()
s = Extension2()
r.Id and s.Id will both be set to "id"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Derek O |

