[Tutor] making onthefly attributes persistent

ALAN GAULD alan.gauld at btinternet.com
Tue Dec 14 01:11:46 CET 2010


The OOP topic in my tutor has a section on persisting objects 
by writing them to a text file. The basic principle being that 
each subclass only persists the attributes that it adds and 
relies on the superclass to persist itself.

In this case you would have to do someting like get the save() 
method to return the list of attributes persisted. Then the bottom 
subclass could see what all the superclasses had saved and 
only save any new attributes not in that list. 

The trick is unwinding that when you come to restore your 
objects. You will need to store the data as name/value pairs so 
that you can read the dynamic attributes, then use setattr() 
or similar to create them dynamically again. The problem is 
how do you know that nobody has created a subclass below 
the bottom level?!" So that leaves you trying to read the entire 
list of attributes in at once at the top level and restore them
which breaks the concept of objects only knowing about their 
own data...

Its all a bit messy, it is so much easier if you don't use 
dynamic attributes.

 Alan Gauld
Author of the Learn To Program website
http://www.alan-g.me.uk/



>
>From: Jojo Mwebaze <jojo.mwebaze at gmail.com>
>To: Alan Gauld <alan.gauld at btinternet.com>
>Cc: tutor at python.org
>Sent: Monday, 13 December, 2010 22:50:49
>Subject: Re: [Tutor] making onthefly attributes persistent
>
>
>
>
>On Mon, Dec 13, 2010 at 8:44 PM, Alan Gauld <alan.gauld at btinternet.com> wrote:
>
>
>>"Jojo Mwebaze" <jojo.mwebaze at gmail.com> wrote
>>
>>
>>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
>>>
>>>
bank.debit = debit
>>>
>>>
>>>#I can also add an attribute owner
>>>
>>>myaccount.owner = 'jojo'
>>>
>
>My problem is how to make the added attributes, 'owner' and 'debit'
>>persistent automatically
>>

If that's your only problem with this approach congratulations!
>How does your orther code know when/if these dynamic
>operations/data exist so as to use them? If they just assume
>they exist then why not just add them in the definition. Even as nulls?
>
>While Python allows you to dynamically add features to classes/objects
>its not something I would recommend unless you have a really good
>reason - not least because you bring upon yourself all sorts of problems!
>
>If you are determined to do so you can make the objects persistent
>using the approach I outline on my tutorial but adding a loop to cycle
>over the contents of dir(). But you may find that recovering the
>objects - especially if they have a mixed set of attribnutes - presents
>even more problems...
>
>IMHO This is a feature of python that should be considered unorthodox
>and only to be used when no other approach will work!
>
>HTH,
>
>
>


Thanks Allan for the feedback, the idea is to write a method like store() on the 
object, that probably  looks up all these changes and commits them into the 
database.

Please let me know where to find approach you propose in your tutorial. I read 
your tutorial when i was just initiated to python, a reference would be helpful 
to help me find the soln without hustle.

Cheers



 
-- 
>Alan Gauld
>Author of the Learn to Program web site
>http://www.alan-g.me.uk/
>
>
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>To unsubscribe or change subscription options:
>http://mail.python.org/mailman/listinfo/tutor
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20101214/5d8cd3ff/attachment-0001.html>


More information about the Tutor mailing list