What is the meaning of static and class methods ?

Pierre-Frédéric Caillaud peufeu at free.fr
Mon Jun 28 04:36:07 EDT 2004


	see :

http://www.python.org/doc/current/lib/built-in-funcs.html#l2h-14

	find classmethod and staticmehtod

copypaste :

*************************************************
staticmethod(	function)	
Return a static method for function.

A static method does not receive an implicit first argument. To declare a  
static method, use this idiom:


class C:
     def f(arg1, arg2, ...): ...
     f = staticmethod(f)

It can be called either on the class (such as C.f()) or on an instance  
(such as C().f()). The instance is ignored except for its class.

Static methods in Python are similar to those found in Java or C++

*************************************************
classmethod(	function)	
Return a class method for function.

A class method receives the class as implicit first argument, just like an  
instance method receives the instance. To declare a class method, use this  
idiom:


class C:
     def f(cls, arg1, arg2, ...): ...
     f = classmethod(f)

It can be called either on the class (such as C.f()) or on an instance  
(such as C().f()). The instance is ignored except for its class. If a  
class method is called for a derived class, the derived class object is  
passed as the implied first argument.

Class methods are different than C++ or Java static methods.




> But I don't understand, why we need class method-s in python. If I want  
> to create an object, that is don't need to I execute this object's  
> Create method.

	it's used to invoke a method on a class without instanciating the object.

> Some other possible meaning is to create some class methods, that  
> creates object with preinitialized state. Example: an ftp object with  
> opened, and setted state.

	could be a GetInstance like behaviour, then, yes.

> But we can do that with simple functions too.

	then you pollute the global namespace

> Another question is that what is the meaning of static methods ?
> In Delphi the not overrided methods of class are static, but you cannot  
> use the Self parameter in that methods.
> In Java, the without static methods the JVM cannot run your main class,  
> so it is needed to execute your program.

	don't understand what you say

> But in py I don't understand the meaning of that methods. In the  
> examples I see that we cannot use self parameter in  static method - it  
> is correct - but everything is seems to be that this is only a method  
> with "special sign" that make the static method the piece of an object.

	Example :

- GetTypeID() returns an integer. This is stored in a MySQL field for  
instance.
Ideal application for a static method because it only depends on the  
object class.

- CreateSubclass() creates a subclass of the desired variant.
Fits well with a class method.

class baseclass( object ):
	def GetTypeID():
		return 0
	GetTypeID = staticmethod( GetTypeID )

	def CreateSubclass( cls, typeid, *args )
		subclasses = { 1:subclass1, 2:subclass2 }
		return subclasses[typeid](args)

class subclass1( object ):
	def __init__(self, .....)
		....

	def GetTypeID():
		return 1
	GetTypeID = staticmethod( GetTypeID )

class subclass2( object ):
	def __init__(self, .....)
		....

	def GetTypeID():
		return 2
	GetTypeID = staticmethod( GetTypeID )


So you get :

typeid,args = get'em from database

obj = baseclass.CreateSubclass( typeid, args )

.. do something with obj...

etc...















More information about the Python-list mailing list