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?

1
  • 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. Commented Feb 20, 2013 at 9:23

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

"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)?..
"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.
0

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

2 Comments

It is not; it is only contained in a class, adding a namespace.
@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).
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):
    # ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.