5

I have read

As staticmethod can't access the instance of that class, I don't know what's the difference betweent it and global function?

And when should use staticmethod? Can give a good example?

Community
  • 1
  • 1
Tanky Woo
  • 4,906
  • 9
  • 44
  • 75
  • 4
    The best answer in my opion is the top comment to the approved answer in your first link: "A staticmethod isn't useless - it's a way of putting a function into a class (because it logically belongs there), while indicating that it does not require access to the class.". You could imaginge the `datetime` class having static methods for specific date related things, but those functions may equally well be at the module level. –  Feb 20 '13 at 09:23

3 Answers3

6

Like global function, static method cannot access the instance of the containing class. But it conceptually belongs to the containing class. The other benefit is it can avoid name confliction.

When the function is designed to serve for some given class, it's advisable to make it as a static method of that class. This is called cohesion. Besides, if this function is not used outside, you can add underscore before it to mark it as "private", this is called information hiding(despite Python doesn't really support private methods). As a rule of thumb, exposing as little interfaces as possible will make code more clean and less subject to change.

Even if that function is supposed to serve as a shared utility for many classes that are across multiple modules, making it a global function is still not the first choice. Consider to make it as some utility class's static method, or make it a global function in some specialized module. One reason for this is collecting similar-purposed functions into a common class or module is good for another level's abstraction/modularization(for small projects, some people may argue that this is overengineering). The other reason is this may reduce namespace pollution.

Hui Zheng
  • 10,084
  • 2
  • 35
  • 40
  • 1
    "Even if that function is supposed to serve as a shared utility for many classes, making it a global function is still not recommended.": That's news to me.. Yes, I know namespaces are a good thing, but where is this recommendation made (I may have missed it)?.. – Demian Brecht Feb 20 '13 at 09:25
  • "Even if that function is supposed to serve as a shared utility for many classes, making it a global function is still not recommended" => well, actually, yes, it is recommended to use a plain function when you don't have a good reason to go for a staticmethod. wrt/namespaces, modules are namespaces already. – bruno desthuilliers Feb 20 '13 at 12:23
0

A static method is contained in a class (adding a namespace as pointed out by @MartijnPieters). A global function is not.

Demian Brecht
  • 21,135
  • 5
  • 42
  • 46
  • It is not; it is only *contained* in a class, adding a namespace. – Martijn Pieters Feb 20 '13 at 09:17
  • @MartijnPieters: I mis-worded that somewhat.. Updated. My intention in saying it was bound to a class wasn't as clear as your description (as there can be confusion between bound to a class and bound to an **instance** of a class, which is not what I meant at all). – Demian Brecht Feb 20 '13 at 09:20
0

IMO it is more of a design question, rather than a technical one. If you feel that the logic belongs to a class (not the instance) add it as a staticmethod, if it's unrelated implement it as a global function.

For example:

class Image(object):
    @staticmethod
    def to_grayscale(pixel):
        # ...

IMO is better than

def to_grayscale(pixel):
    #...

class Image(object):
    # ...
dmg
  • 7,438
  • 2
  • 24
  • 33