I'd like to know if it's possible to have two methods in a class with the same name (or only one if it can do what I want) but the first method would be called on class().method()
and the second one on class.method()
(example below).
I tried to do it by creating a method with @staticmethod
and a second one (with the same name) without.
Here is my example:
class MyClass:
# @potential_decorator1
def my_function():
print("MyClass.my_function() has been called !")
# @potential_decorator2
def my_function(self):
print("Now it's MyClass().my_function() that has been called !")
What I tried:
class MyClass:
@staticmethod
def my_function():
print("MyClass.my_function() has been called !")
def my_function(self):
print("Now it's MyClass().my_function() that has been called !")
But when I tested it:
>>> MyClass.my_function()
TypeError: my_function() missing 1 required positional argument: 'self' #I'd like the
#staticmethod to be called instead
>>> MyClass().my_function()
Now it's MyClass().my_function() that has been called !
By analyzing it, I understood that the "classic" method overrides the static method.