469,588 Members | 2,734 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

way to define static method

I think Python uses a very strange way to define static method in a
class. Why not make it like this?

class MyClass:
def my_static_method(self):
# self should be None as it's a static method
# just ignore self

I'm a newcomer so maybe it's quite naive. But I just wonder why it is
designed like this.

Thanks.
Aug 22 '07 #1
1 1071
On Wed, 22 Aug 2007 20:34:21 +0100, Eric CHAO wrote:
I think Python uses a very strange way to define static method in a
class. Why not make it like this?
What is so strange about it?
class MyClass:
def my_static_method(self):
# self should be None as it's a static method
# just ignore self

I'm a newcomer so maybe it's quite naive. But I just wonder why it is
designed like this.
That method above isn't a static method, so why should `self` be None?
Static methods are created with the `staticmethod` function which can be
used as decorator:

class MyClass(object):
@staticmethod
def my_real_static_method():
# Look ma, no `self`. :-)

Static methods are just functions, on classes `classmethod`\s are often
more useful. They get the class as first argument.

Ciao,
Marc 'BlackJack' Rintsch
Aug 22 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by John Ratliff | last post: by
14 posts views Thread by Carl Ribbegaardh | last post: by
15 posts views Thread by ad | last post: by
8 posts views Thread by ad | last post: by
5 posts views Thread by feel_energetic | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.