What is the meaning of static and class methods ?

F

fowlertrainer

Hi !

I read the MetaClasses / Unifying Howto in python.org, but I cannot
understand many things.

I use Delphi - I use many classes in it, and I know the Java class-es too.

In Delphi the class method is a method of class that callable without
class creation.
In Delphi the object creation is used class method named Create.

Button:=TButton.Create(Owner);

The Create is working on class, not on created object, and it is create,
and return the created object.
This is the only way to create objects.

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.
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.
But we can do that with simple functions too.

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.

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.

So:
I want to anyone please send an example of class/static methods that
HAVE MEANING.
A real example, to I understand, why we need that methods in python.

Thanx for it:
FT
 
?

=?iso-8859-15?Q?Pierre-Fr=E9d=E9ric_Caillaud?=

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...
 
M

Michele Simionato

<snip>
I want to anyone please send an example of class/static methods that
HAVE MEANING.
A real example, to I understand, why we need that methods in python.

Thanx for it:
FT

I may sound heretic, but I never thought their introduction as builtins
was a particularly clever idea. Python lived pretty well without them for
years and could continue living without them. They are just a syntactic
convenient which is not very convenient, actually, since we do not have
decorators yet.


Michele Simionato
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top