Thread
Object Oriented Programming in Python
Methods in Python (with code snippets)
In this thread, we will discuss 3 types of methods with examples
1. Instance
2. Class
3. Static
A Thread ๐งต๐
Methods in Python (with code snippets)
In this thread, we will discuss 3 types of methods with examples
1. Instance
2. Class
3. Static
A Thread ๐งต๐
1. Instance methods:
These methods are bound to the instance(object) of the class.
Used to access/modify the object state. If we use instance variables inside a method, such methods are called instance methods. It must have a self parameter to refer to the current object
These methods are bound to the instance(object) of the class.
Used to access/modify the object state. If we use instance variables inside a method, such methods are called instance methods. It must have a self parameter to refer to the current object
2. Class Methods
Class methods work with class variables and are accessible using the name rather than its object.
@classmethod decorator is used to create class methods. cls is used to refer to the class just like self is used to refer to the object of the class.
Class methods work with class variables and are accessible using the name rather than its object.
@classmethod decorator is used to create class methods. cls is used to refer to the class just like self is used to refer to the object of the class.
3. Static Methods
usually limited to class. They have no direct relation to class or instance variables. They are used as utility functions inside the class or when we don't want the inherited classes.
`@staticmethod` decorator is used to define static methods.
usually limited to class. They have no direct relation to class or instance variables. They are used as utility functions inside the class or when we don't want the inherited classes.
`@staticmethod` decorator is used to define static methods.
Find the source code of this thread ๐
Star the repo if you like the content. โญ๏ธ
github.com/afizs/python-notes/blob/main/basics/OOP_In_Python.ipynb
Star the repo if you like the content. โญ๏ธ
github.com/afizs/python-notes/blob/main/basics/OOP_In_Python.ipynb
In future threads, we are going to break down each method type.
Follow me @itsafiz if you don't want to miss it.
Follow me @itsafiz if you don't want to miss it.
Mentions
See All
Rohit Ghumare @ghumare64
ยท
Nov 26, 2022
Awesome thread