Reputation: 607
so I have a class Model and inside model I have a method save
def save(self):
args = list(map(self.getValueOrDefault,self.__fields__))
args.append(self.getValueOrDefault(self.__primary_key__))
logging.debug('saving %s' % str(args))
row = yield from execute(self.__insert__,args)
if row != 1:
logging.debug('failed to insert record: affected rows: %s' % row)
else:
logging.info('insertion was succesful')
here is the code for getValueOrDefault, they are in the same class
def getValueOrDefault(self,key):
value = getattr(self,key)
if value is None:
field = self.__mapping__[key]
if field.default is not None:
value = field.default() if callable(field.default) else field.default
logging.info('using default value for %s ==> %s' % (key,value))
setattr(self,key,value)
return value
if I add classmethod to save and call save from an instance I get an error saying getValueOrDefault requires additional positional argument 'key'. My guess is when I call it from a instance the self (which becomes a cls) is the class itself not the instance created and I'm trying to do Model.getValueOrDefault but shouldn't it give me some error that tells me I can't call a function without initializing the class?
Upvotes: 0
Views: 113
Reputation: 280544
My guess is when I call it from a instance the self (which becomes a cls) is the class itself not the instance created
Yup.
and I'm trying to do Model.getValueOrDefault but shouldn't it give me some error that tells me I can't call a function without initializing the class?
Nope. It's entirely valid to call Model.getValueOrDefault
, but you have to pass self
explicitly when you do that.
Upvotes: 1