This article was first published on wechat: Python Programming Time

The title “Static methods are hidden secrets” is just one point of the article. There may be some clickbait, but that’s ok, I’m sure there are a lot of people out there who haven’t studied them very deeply, don’t believe me, let me ask you three questions, see if you can answer them.

1. What is the difference between functions and methods in Python2.x and 3.x?

2. With class/instance methods and ordinary functions, why static methods?

How many static methods can be written in python3. x?

With these three questions in mind, you can try to find answers below.


The distinction between functions and methods in Python 2 is clear and easy to distinguish. But in Python3, I found a completely different set of rules.

Python2 first (Python2.7)

It can be concluded that:

1, ordinary functions (not located in the class), are functions.

Staticmethod (@staticmethod), both functions.

Class methods (@classMethod) are all methods.

Instance methods (which start with self and are not static or class methods) are all methods.

You might want to say, class methods and instance methods, are methods, after all, the name of the method itself, ordinary functions are functions, I also understand. So static methods, why not methods but functions?

The name is just an external name, can you say “Zhao Tienan” must be a man?

A method is a function bound to an object (instance or class).

The first argument to a class method is CLS, which is called without displaying the incoming. The instance method takes self as its first argument, and when called, it does not need to display the pass.

Static methods, on the other hand, are no different from ordinary functions, except that the position they define is placed in the class as a function of the class or instance.

Then you must ask, why put a static method in a class when it is the same as a normal function?

I said the above, in the class definition, you can let it become a tool of class function, it’s like you carry a knife, the knife himself has no direct relationship with you, the only contact is this knife is yours, and you bring it in the body, no matter where you go, as long as needed, you can directly out. What are the disadvantages of putting functions outside the class? When you go out (in another module) and find that you need a knife, you have to make a special trip to the store to buy one (import this function).

In addition, I think the static approach makes more business and design sense.

A static method is a utility function for a class or instance, such as checking the validity of a variable, serializing or deserializing data, etc.

Python2, Python3.

I used to think that Python2 had a clearer distinction between methods and functions, but with Python3, the distinction between methods and functions seems to make more sense.

Again, I changed the interpreter to Python3.6.

The only difference from Python2 is that people. jump becomes a function in Python3.

Does that upend the knowledge you’ve just built up?

Don’t worry. I’ll run another experiment, and maybe you’ll find out.

In Python2

Jump (‘hello’) returns an error saying that the first argument to jump must be an instance of People, which is understood because the first argument to jump is self.

In Python3

As you can see, the first argument to jump is no longer required to be an instance of People, but can be any object, such as if I used a string object, and no error was reported.

That is, when you pass in an instance of People, jump is a method, and when you pass in an instance of People, jump is a function.

You see how flexible it is.

To recap, in Python3:

1, ordinary functions (not located in the class), are functions.

Staticmethod (@staticmethod), both functions.

Class methods (@classMethod) are all methods.

4. The distinction between methods and functions is not so clear, but more flexible. A function may be a method or a function.

So, again, does that mean that Python3 doesn’t have to use @staticmethod for static methods, because Python3 recognizes them anyway?

That’s a good question. Yes, you don’t have to specify it, but it’s better to specify it, because if you don’t specify it, you can only call this method with people. jump, not self, because the first argument is not self, whereas with @staticMethod you can use self.

So it’s a specification, like a private method of a class, and the specification says it’s best not to call it from outside, but it’s not mandatory, it’s not like you can’t call it from outside.

The origin of writing this article is that two days ago, a reader asked a related question in the exchange, there was no topic to write, so I took it as material to sort out, also happened to have not written static method, class method content, I did not expect that simple things can write so much content.