Reputation: 7
I'm new to Python and just learning about its implementation of objects/classes. I understand the difference between an instance method, class method, and static method, I think, but what I don't understand is why a method that has not been decorated as a @classmethod or @staticmethod can be called from the class itself.
My very (very) basic example:
class TestClass:
def __init__(self):
pass
@staticmethod
def print_static_stuff(stuff):
print(stuff)
def print_stuff(stuff):
print(stuff)
TestClass.print_stuff("stuff") # prints "stuff"
TestClass.print_static_stuff("static stuff") # prints "static stuff"
The method print_stuff() seems to act as a static method when called on the class, taking the argument as it's single parameter and not the class (cls). Why can the method not decorated with @staticclass be called on the class? Is this by design or just a weird side-effect, and why? From what I've learned so far, Python, by design, has few to no "weird side-effects".
Upvotes: 0
Views: 54
Reputation: 69924
The first parameter being named self
is merely a convention. The instance will be passed as the first positional argument independent of what you've named it (in this case you've called it stuff
).
Upvotes: 1