[Tutor] making onthefly attributes persistent

Jojo Mwebaze jojo.mwebaze at gmail.com
Mon Dec 13 09:32:22 CET 2010


Hey Tutor

Assuming i have a class bank as below .

class bank(object):
   def __init__(self, bal=0):
       self.bal = bal
   def deposit(self, amount):
       self.bal+=amount
       print self.bal

I define a method debit - which i add to the class onthefly

def debit(self, amt):
   self.bal-=amt
   print self.bal

bank.debit = debit

myacct = bank()
myacct.deposit(1000) # prints 1000
myacct.debit(99) # print 901

#I can also add an attribute owner

myaccount.owner = 'jojo'

dir(myacct) # prints [ ....'owner', 'bal', 'debit', 'deposit']


My problem is how to make the added attributes, 'owner' and 'debit'
persistent automatically

Saving the object using pickle for example does save 'owner' and 'debit'
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20101213/84922570/attachment.html>


More information about the Tutor mailing list