469,923 Members | 1,775 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,923 developers. It's quick & easy.

difference between class and static methods?

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.
Apr 18 '06 #1
3 5483
In <gP******************@news.tufts.edu>, John Salerno wrote:
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
Apr 18 '06 #2
Marc 'BlackJack' Rintsch wrote:
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?
Apr 18 '06 #3

"John Salerno" wrote...
[...]
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
Apr 19 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

18 posts views Thread by John M. Gabriele | last post: by
6 posts views Thread by Baris | last post: by
2 posts views Thread by Vivek Ragunathan | last post: by
17 posts views Thread by =?Utf-8?B?SmVzcGVyLCBEZW5tYXJr?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.