instancemethod

Bruno Desthuilliers bruno.desthuilliers at websiteburo.com
Tue Jan 23 03:26:00 EST 2007


Gert Cuykens a écrit :
> Reading all of the above this is the most simple i can come too.
> 
> import MySQLdb
> 
> class Db:
> 
>    def __init__(self,server,user,password,database):
>        self._db=MySQLdb.connect(server , user , password , database)
>        self._db.autocommit(True)
>        self.cursor=self._db.cursor()
> 
>    def excecute(self,cmd):

Just out of curiousity: is there any reason you spell it "excecute" 
instead of "execute" ?

>        self.cursor.execute(cmd)
>        self.rowcount=int(self.cursor.rowcount)
> 
>    def close(self):
>        self.cursor.close()
>        self._db.close()
> 
>    def __del__(self):
>        try:
>            self.close()
>        except:
>            pass



> if __name__ == '__main__':
>    gert=Db('localhost','root','******','gert')
>    gert.excecute('select * from person')
>    for row in gert.cursor:
>        print row
> 
> This must be the most simple it can get right ?

Using __getattr__ is still simpler.

> PS i didn't understand the __getattr__ quit well but i thought it was
> just to overload the privies class

The __getattr__ method is called when an attribute lookup fails (and 
remember that in Python, methods are -callable- attributes). It's 
commonly used for delegation.



More information about the Python-list mailing list