difference between class and static methods?

J

John Salerno

I've been reading up on them, but I don't quite understand how they
differ in practice. I know how each is implemented, and from C# I
already know what a static method is. But I won't assume that it's the
same in Python. And on top of that, both the class and static methods of
Python seem to do what a C# static method does, so I don't see the
difference yet.

Thanks.
 
M

Marc 'BlackJack' Rintsch

I've been reading up on them, but I don't quite understand how they
differ in practice. I know how each is implemented, and from C# I
already know what a static method is. But I won't assume that it's the
same in Python. And on top of that, both the class and static methods of
Python seem to do what a C# static method does, so I don't see the
difference yet.

The difference is that the classmethod gets the class as first argument
much like self in instance methods.

class A:
def __init__(self, data):
print 'A'
self.data = data

@classmethod
def from_file(cls, filename):
# read data
return cls(data)

class B(A):
def __init__(self, data):
print 'B'
self.data = data

If you call `B.from_file('spam.xyz')` now, the `from_file()` method
inherited from class `A` is called but with `B` as the first argument so
it returns an instance of `B`.

A staticmethod is just a function attached to a class without any "magic".

Ciao,
Marc 'BlackJack' Rintsch
 
J

John Salerno

Marc said:
If you call `B.from_file('spam.xyz')` now, the `from_file()` method
inherited from class `A` is called but with `B` as the first argument so
it returns an instance of `B`.

A staticmethod is just a function attached to a class without any "magic".

So a class method is specifically for using the class name itself as an
object in the method? If that's the case, then it makes some sense now.
I guess the reason I didn't get it before is that this is a feature of
dynamic languages, right? And something that couldn't have been done in
C# anyway?
 
P

Petr Prikryl

...
[...]
So a class method is specifically for using the class name itself as an
object in the method? If that's the case, then it makes some sense now.
I guess the reason I didn't get it before is that this is a feature of
dynamic languages, right? And something that couldn't have been done in
C# anyway?

Yes, almost. You can think about a class method as about something that
can be called even if no object of that class was created. It can be called
through the class name. But it can also be called through the instance
(the object). On the other hand, the normal method can be called only
through the object. It does not belong to the class -- see below.

The class method has access to the class variables, but not to the
instance variables (no instance may exists) -- not shown here.

Try the following script (I have stored it as a.py)
---------------------------------------------------
class C:
def __init__(self):
print 'Instance of the class C was created.'

@classmethod
def myClassMethod(cls):
print 'myClassMethod called'

def myMethod(self):
print 'myMethod (i.e. the normal one) was called.'

C.myClassMethod()

obj = C()
obj.myClassMethod()
obj.myMethod()

C.myMethod() # cannot be done, error.
---------------------------------------------------

And run it

C:\tmp>python a.py
myClassMethod called
Instance of the class C was created.
myClassMethod called
myMethod (i.e. the normal one) was called.
Traceback (most recent call last):
File "a.py", line 18, in ?
C.myMethod()
TypeError: unbound method myMethod() must be called with C instance as first
argument (got nothing instead)

pepr
 

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,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top