class MetaData():
maxSize = 2**10
# class definition code
if not os.path.exists('sample.data'):
SSD = open('sample.data', 'wb+')
data = {
0: [],
1: {'.': None,}
}
data[1]['~'] = data[1]
MetaData.save() # i want to call the save function here
# class function
@classmethod
def save(cls):
cls.SSD.seek(0)
cls.SSD.write(b' ' * cls.maxSize)
cls.SSD.seek(0)
cls.SSD.write(pickle.dumps(cls.data))
I want to use the save() function inside the class block. I've tried MetaDate.save() and simply save() both of which throw errors
Is there any way to achieve this?
Edit
Yes, maxSize is a class var and yes i can access it using cls.maxSize.
At the point when you call
save, the class is not yet defined. One way to get around this is to use inheritance and definesavein the base-class:Then of course, variables in the head of
Aare not known inB.saveand have to be given as arguments tosave.Well, or like this: