0

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".

  • See what `TestClass.print_self('hello')` does, and bask in enlightened bliss... – juanpa.arrivillaga Aug 28 '17 at 00:32
  • 1
    In other words, "methods" are merely normal functions. However, *if accessed through the instance*, they **implicitly pass the instance to the first parameter**. Note, the name `self` is just a *convention*. You can name the first argument whatever you want. – juanpa.arrivillaga Aug 28 '17 at 00:38
  • Check out [this answer](https://stackoverflow.com/a/45645637/5014455) to another question, where I try to explain this with some examples. Also, realize that that `TestClass.print_stuff` returns a function object: ``, wheras `t.print_stuff` returns a *bound method object*: `>`. – juanpa.arrivillaga Aug 28 '17 at 00:44
  • If you want to see what is going on under the hood, [this](http://emmanuel-klinger.net/pythons-attribute-descriptors.html) is a blog post that explains it be re-implementing `Function` and `Method` objects in Python using descriptors, which is how it is actually implemented under the hood!. – juanpa.arrivillaga Aug 28 '17 at 00:45
  • I do understand instance methods, my question is about class methods. I've revised my question to make that more clear. Thanks for the replies so far! – SpacemanSpiff Aug 28 '17 at 13:57
  • No you *clearly do not* understand instance methods, or else you would understand why `TestClass.print_stuff('stuff')` actually prints `"stuff"`. **All methods belong to the class**. Methods in Python are merely *plain functions in the class's namespace* that get *implicitely bound to an instance* (using the descriptor protocol underneath the hood). Again, I urge you to read the various links I've posted. – juanpa.arrivillaga Aug 28 '17 at 15:43
  • @juanpa.arrivillaga I get what you're saying, but again I think you're missing my point. I am NOT talking about functions or methods called from instances but straight from the class definition without even creating any objects based on the class. Is it safe to say a class definition can act simply as a namespace within code, even if it is never instantiated? Not a good use of classes, perhaps, but a use nonetheless? – SpacemanSpiff Sep 06 '17 at 18:43
  • Again, **you do not understand instance methods in Python** if you don't understand that **all methods, including instance methods** belong *to the class* and can be called directly from the class. So yes, it is safe to say that a class can act simply as a namespace. The only thing the `staticmethod` decorator does is makes sure *you do not pass the instance as the first argument* when you call the method *on an instance*. Basically, you can think of `my_instance.my_method()` as syntactic sugar for `MyClass.my_method(my_instance)` – juanpa.arrivillaga Sep 06 '17 at 19:44

1 Answers1

1

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).

anthony sottile
  • 61,815
  • 15
  • 148
  • 207
  • I get instance methods, but am asking about class methods and have revised my question accordingly. Thanks for the answer! – SpacemanSpiff Aug 28 '17 at 13:57