472,125 Members | 1,607 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,125 software developers and data experts.

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 1112
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 Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.