Search code examples
pythonmethodssyntaxself

Python method call syntax shorthand


Is there a reason we call methods in python like object.method instead of Class.method(object)?

Maybe it isn't a strange choice, but personally it made understanding the self parameter much easier when I was shown the second way of calling a method.


Solution

  • Hardcoding the class name basically prevents you from using polymorphism. This is general OOP, not particularly a Python feature.

    Your calling code should not need to know, nor care, which exact class object is.

    This is immediately a problem for code where object can be a member of either Baseclass or Derivedclass, but much more complex inheritance and method overriding scenarios are possible, and sometimes necessary.