Does Python have Class methods
James_Althoff at i2.com
James_Althoff at i2.com
Wed May 9 16:11:18 EDT 2001
Magnus writes:
:This is all very fancy and nice, but what is wrong with the following?
:
: def double(self, x):
: return x*2
:
: class Doubler:
: double = double
:
: d = Doubler()
: d.double(3)
: # returns 6
The goal is to be able to write:
Doubler.double(3)
not
Doubler().double(3)
Your example yields:
>>> Doubler.double(3)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: unbound method must be called with class instance
>>>
IOW you want a "class method" -- preferably meaning "a method defined for
and invoked on a class object independent of any instance of said class".
Jim
More information about the Python-list
mailing list