Assume there are two files of Python, the first file defines a class:
File1: test1.py
class C1(object):
def __init__(self):
pass
def method1(self):
global v1
v1 = "cat"
self.method2()
def method2(self):
print(v1)
The second file define another class, which is derived from the first class from the first file:
File2: test2.py
from test1 import C1
class C2(C1):
def __init__(self):
super(C2, self).__init__()
def method1(self):
global v1
v1 = "dog"
self.method2()
The following is the test code:
c2 = C2()
c2.method1()
An error will occur:
NameError: name 'v1' is not defined
I want to overwrite the method 'method1' only, how can the global variable 'v1' in C2 be accessed by the method2 in C1?