[Tutor] static method

Sergio Murru smu@archesis.it
Wed, 08 Dec 1999 16:18:39 +0100


My background is c++, so I tried to implement a static method like this

--------------------------------------------------------------
class MyClass:
        def staticMethod():
                print 'hi from static method'

MyClass.staticMethod()
--------------------------------------------------------------

but i get a
"TypeError: unbound method must be called with class instance 1st argument". 

I understand that this makes sense when I try 

--------------------------------------------------------------
class MyClass:
        def method():
                print 'hi from method'

MyClass.method() 

# instead of 

myclass_instance = MyClass()
MyClass.method(myclass_instance) 
#or
myclass_instance.method()
--------------------------------------------------------------

MyClass.method() without 
or 
myclass_instance.method()

I also understand that I can use static memebers like this:

-------------------------------------------------------------
class MyClass:
	memberstatic = 4
	
	def metodo(self):
		print MyClass.memberstatic
--------------------------------------------------------------

The question is: How can I implement a static method in python??

Thanks