What is the Python equivalent of a Ruby class method?

There are two ways to do this:

@staticmethod
def foo():     # No implicit parameter
    print 'foo'  


@classmethod
def foo(cls):   # Class as implicit paramter
    print cls

The difference is that a static method has no implicit parameters at all. A class method receives the class that it is called on in exactly the same way that a normal method receives the instance.

Which one you use depends on if you want the method to have access to the class or not.

Either one can be called without an instance.


What you're looking for is the staticmethod decorator, which can be used to make methods that don't require a first implicit argument. It can be used like this:

class A(object):
    @staticmethod
    def a():
        return 'A.a'

On the other hand, if you wish to access the class (not the instance) from the method, you can use the classmethod decorator, which is used mostly the same way:

class A(object):
    @classmethod
    def a(cls):
        return '%s.a' % cls.__name__

Which can still be called without instanciating the object (A.a()).