Am 13.12.2010 23:50, schrieb Jojo Mwebaze:


On Mon, Dec 13, 2010 at 8:44 PM, Alan Gauld <alan.ga...@btinternet.com
<mailto:alan.ga...@btinternet.com>> wrote:


    "Jojo Mwebaze" <jojo.mweb...@gmail.com
    <mailto:jojo.mweb...@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.

Assuming you're planning to use a database with a scheme (e.g. a relational DB) you need to define your tables in advance anyway. Even if you're using a schemeless DB, where do you track which data is in it? How do you build your queries dynamically? This can become very messy. Overall, I think Alan's recommendation to avoid dynamic attribute creation is the one to follow.


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@python.org <mailto:Tutor@python.org>
    To unsubscribe or change subscription options:
    http://mail.python.org/mailman/listinfo/tutor




_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to