Ivan Shevanski wrote: >To continue with my previous problems, now I'm trying out classes. But I >have a problem (which I bet is easily solveable) that I really don't get. >The numerous tutorials I've looked at just confsed me.For intance: > > > >>>>class Xyz: >>>> >>>> >... def y(self): >... q = 2 >... > > >>>>Xyz.y() >>>> >>>> >Traceback (most recent call last): > File "<stdin>", line 1, in ? >TypeError: unbound method y() must be called with Xyz instance as first >argument >(got nothing instead) > > >So. . .What do I have to do? I know this is an extremley noob question but I >think maybe if a person explained it to me I would finally get it =/ > > > You have to create an instance of the class before it can be called. Ex: class foo: def bar(self): print "Hello, World!" foo().bar()
This is because foo is a reference to a classobj, however, when you call the class, it becomes an instance. Ex: >>> type(foo) <type 'classobj'> >>> type(foo()) <type 'instance'> When an instance is created it tells Python to pass the instance as the first argument (self). Otherwise it gives it None or something similer. (Note that im not 100% sure about why it does'nt work, im just guessing from the way it _seems_ to work. I have read no documentation on this. If you want it to work before you create an instance, then you can do that with class foo: @classmethod def bar(self): print "Hello, World!" or the older (but exactly the same): class foo: def bar(self): print "Hello, World!" bar = classmethod(bar) >thanks in advance, > >-Ivan > > > HTH, Peter -- http://mail.python.org/mailman/listinfo/python-list