Passing values to a class

Chris Gonnerman chris.gonnerman at usa.net
Tue Feb 27 22:50:00 EST 2001


----- Original Message ----- 
From: "Jay Collins" <jcollin at exis.net>
Subject: Passing values to a class
> Anyone shed some light on what I'm doing stupid/wrong here:

Let me just mark up your code:
 
import dbm
 
 class db:
    def __init__(self,base):
        print "using database",base
        self.base = base
#       pass # not needed
    def createdb(self):
#       d = dbm.open(base,"n")
        d = dbm.open(self.base,"n")  # access the copy of base
                                     # stored in self
        d['name'] = "Jim Smith"
        d.close()
 
 app = db("databse")
app.createdb()
 
> how come createdb() doesn't see base? Maybe I'm not getting 
> class scope correctly. My idea was to pass what database I 
> wanted to work on for that instance. Then I could call the 
> methods to do work on the data in it.

The concept is good, but simply handing base to __init__ 
doesn't actually DO anything; you still need to store it 
somewhere.






More information about the Python-list mailing list