12.10.1. Rationale¶

• Methods are functions in the class

• Prevents copy-paste code

• Improves refactoring

• Decomposes bigger problem into smaller chunks

method

Functions in the class which takes instance as first argument (self)

self

Instance on which method was called.

12.10.2. Syntax¶

>>> class MyClass:
...     def mymethod(self):
...         pass
>>>
>>>
>>> my = MyClass()
>>> my.mymethod()


12.10.3. Define¶

• At definition - self should always be a first parameter

>>> class Astronaut:
...     def say_hello(self):
...         print('hello')


12.10.4. Self¶

• At definition - self should always be a first parameter

• At call - self is not passed as an argument (Python will do that)

• At call - self is not passed as an argument (Python will do that)