You can call the method of the class directly in Python3 without adding @staticmethod

I was surprised to be able to call the method of the class directly without adding @staticmethod in Python3: scream:

To call the method of a normal class, create an object and call it. Or, I think you can call it directly with @classmethod or @staticmethod.

But as the title says, I could call it directly.

environment

Python 3.8.2

Call without @staticmethod

Let's prepare a simple sample. In the code below, @staticmethod is not added, but the foo () method can be called directly.

>>> class A:
...     def foo():         # @static method not attached
...         print('Hello foo')
...
>>> A.foo()
Hello foo       <===I can call you directly for some reason
>>> A().foo()
Traceback (most recent call last):   <===Cannot be called from an object
  File "<stdin>", line 1, in <module>
TypeError: foo() takes 0 positional arguments but 1 was given
>>>

Call with @staticmethod

Now let's put it on. Of course you can call it directly because it has @staticmethod attached, but you can also call it from an object.

>>> class B:
...     @staticmethod      # @attach static method
...     def foo():
...         print('Hello foo')
...
>>> B.foo()
Hello foo
>>> B().foo()
Hello foo       <===Can also be called from an object
>>>

@staticmethod Difference between with and without

As you can see above, it seems that there is only ** whether it can be called from an object **, but I'm not sure.

Until very recently, I didn't know that python3 had such a specification. However, it is easier to understand if you add @staticmethod, so I will continue to add it.

For your reference

Let's see how it was in Python2

environment

Python 2.7.16

Verification

>>> class A:
...     def foo():               # @static method not attached
...         print('Hello foo')
... 
>>> A.foo()
Traceback (most recent call last):   <===Do not call directly
  File "<stdin>", line 1, in <module>
TypeError: unbound method foo() must be called with A instance as first argument (got nothing instead)
>>> A().foo()
Traceback (most recent call last):    <===Of course not from the object
  File "<stdin>", line 1, in <module>
TypeError: foo() takes no arguments (1 given)
>>>
>>>
>>> class B:
...     @staticmethod             # @attach static method
...     def foo():
...         print('Hello foo')
... 
>>> B.foo()
Hello foo           <=== @staticmethod can be called
>>> B().foo()
Hello foo
>>>

Looking at this, Python 2 is no good. But why did Python 3 become OK? concern.

Referenced URL

https://stackoverflow.com/questions/43587044/do-we-really-need-staticmethod-decorator-in-python-to-declare-static-method https://stackoverflow.com/questions/136097/difference-between-staticmethod-and-classmethod

Recommended Posts

You can call the method of the class directly in Python3 without adding @staticmethod
How to use the __call__ method in a Python class
If you define a method in a Ruby class and define a method in it, it becomes a method of the original class.
__init__ called by wxPython or Tkinter was a __init__ call of the inheriting class in Python
Examine the object's class in python
A function that measures the processing time of a method in python
Check the behavior of destructor in Python
If you want a singleton in python, think of the module as a singleton
The result of installing python in Anaconda
I want to explain the abstract class (ABCmeta) of Python in detail.
Hit a method of a class instance with the Python Bottle Web API
The basics of running NoxPlayer in Python
You can see the transition of points in the J League on the graph!
In search of the fastest FizzBuzz in Python
A class for PYTHON that can be operated without being aware of LDAP
You will be an engineer in 100 days --Day 29 --Python --Basics of the Python language 5
You will be an engineer in 100 days --Day 33 --Python --Basics of the Python language 8
You will be an engineer in 100 days --Day 26 --Python --Basics of the Python language 3
In Python, change the behavior of the method depending on how it is called
Visualize the timeline of the number of issues on GitHub assigned to you in Python
You will be an engineer in 100 days --Day 32 --Python --Basics of the Python language 7
You will be an engineer in 100 days --Day 28 --Python --Basics of the Python language 4
Output the number of CPU cores in Python
Get the caller of a function in Python
Match the distribution of each group in Python
Learn the design pattern "Template Method" in Python
View the result of geometry processing in Python
I tried the least squares method in Python
To dynamically replace the next method in python
Make a copy of the list in Python
Learn the design pattern "Factory Method" in Python
Until you run the changefinder sample in python
Find the divisor of the value entered in python
Find the solution of the nth-order equation in python
The story of reading HSPICE data in Python
[Note] About the role of underscore "_" in Python
About the behavior of Model.get_or_create () of peewee in Python
Solving the equation of motion in Python (odeint)
Output in the form of a python array
Try implementing the Monte Carlo method in Python
[Python] Understand the self of the class. Learn the role of self from the execution result with or without self.
A mechanism to call a Ruby method from Python that can be done in 200 lines
Don't take an instance of a Python exception class directly as an argument to the exception class!
Experience the good calculation efficiency of vectorization in Python
How to get the number of digits in Python
Find out the location of Python class definition files.
[Python] No value for argument'self' in unbound method call
Destroy the intermediate expression of the sweep method with Python
[python] Get the list of classes defined in the module
Determine the threshold using the P tile method in python
A python implementation of the Bayesian linear regression class
The story of FileNotFound in Python open () mode ='w'
Learn the design pattern "Chain of Responsibility" in Python
Implement the solution of Riccati algebraic equations in Python
Get the size (number of elements) of UnionFind in Python
Not being aware of the contents of the data in python
Reproduce the execution example of Chapter 4 of Hajipata in Python
Let's use the open data of "Mamebus" in Python
Implemented the algorithm of "Algorithm Picture Book" in Python3 (Heapsort)
[Python] Outputs all combinations of elements in the list
Solve the one-stroke writing (backtrack without recursion in Python)