What is a class method?

P

Paulo da Silva

I understand the concept of a static method.
However I don't know what is a class method.
Would anybody pls. explain me?

class C:
@classmethod
def ...
...

Thanks
 
J

James Mills

I did it before posting ...
The "explanation" is not very clear. It is more like "how to use it".

Without going into the semantics of languages basically the
differences are quite clear:

@classmethod is a decorator that warps a function with
passes the class as it's first argument.

@staticmethod (much like C++/Java) is also a decorator that
wraps a function but does not pass a class or instance as
it's first argument.

I won't go into the use-cases as I don't use static or class
methods myself personally in any of my work (yet).

cheers
James
 
I

Ian Kelly

Em 23-08-2010 04:30, James Mills escreveu:

I did it before posting ...
The "explanation" is not very clear. It is more like "how to use it".

Consider this:

class A(object):
@staticmethod
def new():
return A()

class B(A):
pass

versus this:

class C(object):
@classmethod
def new(cls):
return cls()

class D(C):
pass

B.new() will return a new instance of A, not B. D.new() will return a
new instance of D.

Does this answer your question?
 
T

Terry Reedy

I did it before posting ...

When you ask a question, it help people answer if they know what you
have already tried and failed with ;-)
The "explanation" is not very clear. It is more like "how to use it".

A function accessed as a class attribute is normal treated as an
instance function/method -- with an instance of the class as the first
argument. A class method takes the class as the first argument. A
'staticmethod' is a function that takes neither as the first argument
and, with one esoteric exception, does not need to be a class attribute
but is for convenience.
 
J

John Nagle

Without going into the semantics of languages basically the
differences are quite clear:

@classmethod is a decorator that warps a function with
passes the class as it's first argument.

@staticmethod (much like C++/Java) is also a decorator that
wraps a function but does not pass a class or instance as
it's first argument.

That reads like something the C++ standards revision committee
would dream up as they add unnecessary template gimmicks to the
language.

John Nagle
 
P

Paulo da Silva

Em 23-08-2010 06:16, Ian Kelly escreveu:
Consider this:

class A(object):
@staticmethod
def new():
return A()

class B(A):
pass

versus this:

class C(object):
@classmethod
def new(cls):
return cls()

class D(C):
pass

B.new() will return a new instance of A, not B. D.new() will return a
new instance of D.

Does this answer your question?
Yes. Thank you very much.
 
J

Jean-Michel Pichavant

Paulo said:
Em 23-08-2010 04:30, James Mills escreveu:


I did it before posting ...
The "explanation" is not very clear. It is more like "how to use it".

Thanks anyway.
A very naive approach:

Instance methodes modify/use the instance. They requires a reference to
the instance as first parameter (self)

class Foo:
def foo(self):
print self.instanceAttribute

Class methodes modify/use the class. They require a class as parameter (cls)

class Foo:
occurrences = 0

@classmethod
def foo(cls):
print "Number of %s occurrences : %s" % (cls.__name__,
cls.occurrences)


Static methods neither use a class nor an instance, thus require no
parameter. In that case, the class acts like a namespace (<~>container):

class Foo:

@staticmethod
def sayHello():
print "Hello"

Foo.sayHello()



Cheers,

JM
 

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

No members online now.

Forum statistics

Threads
473,951
Messages
2,570,113
Members
46,698
Latest member
alexxx

Latest Threads

Top