Supernovah
Supernovah

Reputation: 2044

How do I use a class method without passing an instance to it?

I have a common module which consists of a class common which contains processing functions for numbers and types of data I am using. I want to be able to include it like from common import common (or better yet just import common) and use functions like common.howLongAgo(unixTimeStamp).

What is required to do this in my common module?

Upvotes: 23

Views: 43626

Answers (3)

brice
brice

Reputation: 25039

Ways of exposing methods in a python module:

module foo.py:

def module_method():
    return "I am a module method"

class ModClass:
     @staticmethod
     def static_method():
         # the static method gets passed nothing
         return "I am a static method"
     @classmethod
     def class_method(cls):
         # the class method gets passed the class (in this case ModCLass)
         return "I am a class method"
     def instance_method(self):
         # An instance method gets passed the instance of ModClass
         return "I am an instance method"

now, importing:

>>> import foo
>>> foo.module_method()
'I am a module method'
>>> foo.ModClass.static_method()
'I am a static method'
>>> foo.ModClass.class_method()
'I am a class method'
>>> instance = ModClass()
>>> instance.instance_method()
'I am an instance method'

If you want to make class method more useful, import the class directly:

>>> from foo import ModClass
>>> ModClass.class_method()
'I am a class method'

You can also import ... as ... to make it more readable:

>>> from foo import ModClass as Foo
>>> Foo.class_method()
'I am a class method'

Which ones you should use is somewhat a matter of taste. My personal rule of thumb is:

  • Simple utility functions that generally act on things like collections, or perform some computation or fetch some resource should be module methods
  • Functions related to a class but that do not require either a class or an instance should be static methods
  • Functions that are related to a class, and will need the class for comparison, or to access class variable should be class methods.
  • Functions that will act on an instance should be instance method.

Upvotes: 55

Chris Allen
Chris Allen

Reputation: 1

class Test(object):
    @classmethod
    def class_method(cls):
        return "class method"

test = Test()
print test.class_method()

[callen@odin scar]$ python ~/test.py
class method

The above is the standard way of doing classmethods using modern Python objects, not the old-style classes.

Alternately, you might mean a staticmethod:

class Test(object):
    @staticmethod
    def static_method():
        return "static method"

test = Test()
print test.static_method()
[callen@odin scar]$ python ~/test.py
static method

Use whichever makes more sense to you for your problem. Staticmethods should usually be separated into their own independent functionality, the use of a class is superfluous there.

Cf. http://pyvideo.org/video/880/stop-writing-classes

Upvotes: -1

Luka Rahne
Luka Rahne

Reputation: 10457

if you have module common.py and function is in class common

class common(object):
    def howLongAgo(self,timestamp):
          some_code

then you should change your method to be static method whit decorator @staticmethod

class common(object):
    @staticmethod
    def howLongAgo(timestamp): # self goes out
          some_code

This way you do not need to change whole class and you can still use self.howLongAgo in class

Upvotes: 1

Related Questions