Let's assume I have two classes:
#!/usr/bin/env python
class First():
glob = 'Global data'
def __init__(self, arg1, arg2):
self.arg1 = arg1
self.arg2 = arg2
print (arg1, arg2)
class Second(First):
def __init__(self):
print (self.glob)
And they are called from script as:
#!/usr/bin/env python
import classes
data1 = 'First data part'
data2 = 'Second data part'
inst1 = classes.First(data1, data2)
inst2 = classes.Second()
This works, OK:
$ ./script.py
('First data part', 'Second data part')
Global data
I want to make arg1 and arg2 in First class something 'global', and use then it in Second:
class Second(First):
def __init__(self):
print (self.glob, self.arg1, self.arg2)
How I can achieve it?
__init__ofFirstare per-instance attributes; instances of another class (even a subclass) won't see those. What values would instances ofSecondever have for those attributes?First.__init__yourself (since you are overriding it in the child class), possibly via thesuperfunction. After this super call, you will have access to those attributes.