python class question

Jonathan Hogg jonathan at onegoodidea.com
Fri Jul 26 03:53:09 EDT 2002


On 25/7/2002 19:38, in article Xns9256D2197BD87cliechtigmxnet at 62.2.16.82,
"Chris Liechti" <cliechti at gmx.net> wrote:

>> class xrml(SimpleItem.SimpleItem):
>>      def init_db(self):
>>           try:
>>                dbc = getattr(self, connection)
> 
> why not:
> dbc = self.connection

Because that won't work. Look again at the code above. It gets an attribute
of self with the *name* stored in 'connection'. I don't know where
'connection' comes from here. The code that you've shown us Axel would never
have worked, so I'm guessing it is the result of playing with the original
code.

I'm guessing it should have read something along the lines of:

    dbc = getattr( self, self.connection )

presumably Zope (warning: I know nothing about Zope) fills in
'self.connection' with the name of the connection method that should be
called so that the code can be database independent (though that could have
been achieved much more simply).

>>           except:
> 
> one should only catch specific exceptions, otherwise you'll make you life
> harder (by difficult to debug problems)
> 
> better would be:
> except AttributeError:

The error is unlikely to be an AttributeError, it's more like to be some
kind of database connection error. Presumably the Zope machinery wouldn't
fill in the object with an incorrect database connection name/method.

>> Now I tried to modify the last line as follows:
>>           self.DB = dbc()
>> and getting a very strange error (Error Type: UnpickleableError
>> Error Value: Cannot pickle <type 'type'> objects).
> 
> don't know Zope but it look like that it uses pickle to store your instaces
> and the DB object cannot be picked. thats probably because the connection
> to a database cannot be stored, it must be terminated and rebuild, but
> thats doesn't seem to be supported by the db object.

Yup, that looks likely.

>> Now my question is: what the hell am I doing here? Especially what is
>> DB?? connection seems to be equally important, how can I find out what
>> type of thing this is? A "return self.connection" delivers a
>> "MySQL_database_connection". Is this a string, or an object?
> 
> that's an object. it just that the opjects __str__ or __repr__ method
> returns a string for display purposes.

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.

>> The code works well (queries and all else) if I use DB instead of
>> self.DB, but this makes me unable to use DB from other member
>> functions, doesn't it? (I'm quite a Python newbie i fear ... :-))

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.

Anyway, this seems a confusing idiom for getting a database connection. I
think you're missing something simple. Are you even meant to talk to the
database? Isn't the point of object persistence that it manages the database
for you?

Ho hum.

Jonathan




More information about the Python-list mailing list