python class question

Chris Liechti cliechti at gmx.net
Thu Jul 25 14:38:11 EDT 2002


Axel Bock <news-and-lists at the-me.de> wrote in
news:ahpcfo$uor7e$1 at ID-138381.news.dfncis.de: 

> Hi folks,
> 
> I need an explanation of a piece of code *urgently*

usualy you'll get prompt answers here, even if you don't mention it. :-)

>, to understand an
> error message generated by Zope Python (a pure Python error, I think
> ...). 
> 
> Well: I have a class xrml, which wants to access a ZMySQLDA database
> connection (all this happens under Zope, but I think my question is
> pure Python nonetheless). For this I stole code from another module
> (ZSQLMethods :-), and the result looks like this: 
> 
> class xrml(SimpleItem.SimpleItem):
>      def init_db(self):
>           try:
>                dbc = getattr(self, connection)

why not:
    	dbc = self.connection

>           except: 

one should only catch specific exceptions, otherwise you'll make you life 
harder (by difficult to debug problems)

better would be:
    	except AttributeError:


>                return 'error'
>           DB = dbc()

that just binds the result of a call or constructor to the variable DB.

> 
> 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.
 
> 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.

> 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 ... :-))

simplest thing will be when you pass around the DB object as argument if 
you need to call other methods.

maybe someone else can say how DB connections and pickle behave within 
Zope...

chris

-- 
Chris <cliechti at gmx.net>




More information about the Python-list mailing list