Python: 3.8.1
I am unable to append data in class variables using global() function.
class test():
__cv__ = []
def testglobalmethod(self, data):
globals()['__cv__']=[data]
print(__cv__)
rrr = test()
rrr.testglobalmethod("1")
rrr.testglobalmethod("2")
Expected Results:- ['1','2']
Actual Results:- ['1'] ['2']
The append function produces below error:-
def testglobalmethod(self, data):
globals()['__cv__'].append(data)
print(__cv__)
Error: KeyError: '__cv__'
__cv__isn't global, it's a class attribute. Why do you expect to be able to access it viaglobals? Is it intentional that all your code is part of thetestclass scope?cv, or_cvif you want to emphasize that it is for use by methods of thetestclass alone.