Re: instancemethod



"Gert Cuykens" <gert.cuykens@xxxxxxxxx> escribió en el mensaje news:ef60af090701211416n5b2eb7edt2ee6d4555bd49c1a@xxxxxxxxxxxxxxxxx

class Db:

_db=-1
_cursor=-1

@classmethod
def __init__(self,server,user,password,database):
self._db=MySQLdb.connect(server , user , password , database)
self._cursor=self._db.cursor()

@classmethod
def excecute(self,cmd):
self._cursor.execute(cmd)
self._db.commit()


if __name__ == '__main__':
gert=Db('localhost','root','******','gert')
gert.excecute('select * from person')
for x in range(0,gert.rowcount):
print gert.fetchone()
gert.close()

Besides your specific question that was already answered, why are you using classmethods at all?
You are constructing a Db instance and using it as if all were normal instance methods... Just remove all those @classmethod declarations and use it in a standard way.

--
Gabriel Genellina


.



Relevant Pages

  • Re: Unification of Methods and Functions
    ... >> I haven't added any classmethod examples to my OOP chapter, ... >> with an instance method or a static method. ... The most obvious one is to write factory methods: ... > def resize: ...
    (comp.lang.python)
  • Re: Decorator syntax
    ... This problem arose because classmethod and staticmethod weren't built ... fundamental construct, we need language support. ... def classmethod foo: ... Doing such a thing, IMO, should not have syntax support. ...
    (comp.lang.python)
  • Re: Need advice on python importing
    ... def d1: ... Notice how the d1 method on class D uses a classmethod c1 on C. C is in ... instead of passing these in as parameters, ... So, partials won't work. ...
    (comp.lang.python)
  • Re: Python Module Exposure
    ... functions/methods because they are essentially alternative constructors of ISets -- would perhaps better be defined as classmethods of ISet; that's a common way to define instance factories in python. ... I like the idea of lengthening the factory function names, but I'm not sure that the factory methods would be better as class methods. ... def greaterThan: ...
    (comp.lang.python)
  • classmethods, class variables and subclassing
    ... These are set by a classmethod as in the following (in reality the setcvar method ... def setcvar1: ... If I override the setcvar1 method to do some new things special to this ...
    (comp.lang.python)