When python struggles: Attribute Errors

matt matt at greenroom.com.au
Tue Dec 17 17:27:55 EST 2002


Cliff Wells <LogiplexSoftware at earthlink.net> wrote in message news:<mailman.1040147656.20827.python-list at python.org>...
> On Tue, 2002-12-17 at 05:22, matt wrote:
<snip>
> >         if not self.status("Connecting to host: %s..." % host, \
> >                            self.connect(host, user, password)):
> >             ...
> >         self.cursor = self.db.cursor()
</snip>
<snip>
> > It's frustrating because if i were using C++ i could simply type "db =
> > (MySQLdb_Connection*)0;" (or whatever the connection object would be
> > called).
> 
> And then you would call cursor = db.cursor?  Sounds fishy.  If an object
> fails in its initialization, continuing as if nothing had gone wrong
> seems like a bad idea.  If the "DBInitializer" object fails to
> initialize, what good is it?
> 
</snip>

Actually, inside the if statement was a return; call. I apologise for
leaving it out.

<snip>
> I'd write it as:
> 
> class DBInitializer:
>     def init(self, db_name, host, user="", password=""):
>         # Connect to host
>         if not self.status("Connecting to host: %s..." % host, \
>                            self.connect(host, user, password)):
>         ...
>         self.cursor = self.db.cursor()
>         ...
> 
>     def connect(self, host, user, password):
>         self.db = MySQLdb.connect(host, user, password)
> 
> and then put the instantiation of DBInitializer in a try/except
> statement in the calling code.
</snip>

I appreciate your help but unfortunately the attribute error is still
called regardless of self.db's state (None or not None).



More information about the Python-list mailing list