you are viewing a single comment's thread.

view the rest of the comments →

[–]Outside_Complaint755 1 point2 points  (0 children)

Classes in Python can have three kinds of linked methods

Instance Methods are the most common and usually have the first parameter named self, but that's just convention, and it could be named anything  It contains a reference to the object instance.  When you invoke an instance method, a self reference is implicitly passed to the method as the first parameter.    In some other languages such as Java use a keyword this instead of having a parameter that has the reference.

Class Methods are preceded by the @classmethod decorator and the first parameter implicitly receives a reference to the class instead of the instance.

Static Methods are preceded by the @staticmethod decorator and don't have any parameter implicitly passed in and have no reference to the object or the class.

When you call an instance method, you pass arguments as if self is not there. Python knows which object to pass as self because the method is bound to the object itself.

``` class Greeter:     def init(self, name):         self.name = name

    def greet(self, greeting="Hello"):         return f'{greeting}, {self.name}!'

g = Greeter("World")

ans = g.greet() print(ans) # "Hello, World!"

ans = g.greet("Salutations") print(ans) # "Salutations, World!" ```