By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,660 Members | 1,299 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,660 IT Pros & Developers. It's quick & easy.

way to define static method

P: n/a
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
Share this Question
Share on Google+
1 Reply

P: n/a
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.