python class question

Jonathan Hogg jonathan at onegoodidea.com
Fri Jul 26 17:27:23 EDT 2002


On 26/7/2002 18:32, in article ahs0to$v8ku5$1 at ID-138381.news.dfncis.de,
"Axel Bock" <news-and-lists at the-me.de> wrote:

> But what am I trying to do? I try to assign the reference to a new object
> to a member variable - so why does this not work??

Well presumably, and like I say I'm no Zope expert, Zope saves your objects
into the database by grabbing the instance variables and "Pickling" them,
i.e., writing out the data in such a way that it can re-create the original
object again later when it's needed.

Database connections can't be saved in such a way, so when you store a
database connection into an instance variable of your object it makes it
impossible for Zope to save it. Hence the Pickling exception.

>> No, I'd say (as above) that 'self.connection' is a string, which means
>> that 'self' has a method (or instance variable that refers to a class?)
>> called 'MySQL_database_connection'. See what
>> 'self.MySQL_database_connection' is.
> 
> This is an instance of "Connection". But why can "dbc()" deliver an
> instance of "DB" ??? (DB is a class, I checked)

Erm, because that's it's job I guess. You call that to get a connection.

>> Then perhaps you should just do the same in the member functions, i.e.,
>> call the machinery that creates a database connection. You could always
>> wrap it in a method that returns a database connection.
> 
> I can always ... how? ;-))

Well just put the code you originally wrote into a method something like so:

    def getDB( self ):
        dbc = getattr( self, self.connection )
        DB = dbc()
        return DB

Then use this in your methods when you need to talk to the database:

    def doSomething( self, withThis ):
        DB = self.getDB()
        result = DB.query( withThis )
        return result.getOne()

or something along those lines.

I'm not sure I quite understand what you're trying to do in the first place
though. Isn't the point of Zope that it manages the database for you and
provides an object persistence mechanism?

Jonathan




More information about the Python-list mailing list