Static vs Class vs Instance Method In Python: 6 Differences (3 Min)

preview_player
Показать описание
In this tutorial, you'll learn 6 differences between static method, class method & instance method in Python.



Video Transcript:


Hi guys, this is Abhi from Gokcedb. In this video, you're going to learn six differences between a class method, static method, and instance method in Python. Let's start by looking at an example.

Here I'm defining a class called MySQL and using a special underscore underscore init underscore underscore method to initialize three variables. Online 10, I'm specifying an alternate constructor which will allow the construction of a new object by only specifying two parameters. Within the method, I'm using the split function to split the full name into first and last names.

On line 19 I'm defining a utility method to check whether the number of tables is valid or not. On line 27, I'm defining an instance method to print the first name of the DB owner. Now let's get into the differences.

A class method is tied to the class and CLS is the first argument. A class method can modify the class state and it's usually used to create factory methods or alternative constructors. This type of method can access class variables and a class method decorator is used to declare them.

A static method on the other hand is also tied to the class but no CLS or self-keyword is required as the first argument. A static method cannot modify class or object state and it's usually used to create utility methods. This type of method cannot access class or instance variables either.

Add static method decorator is used to declaring them. Let's look at an instance method. An instance method is tied to the object and the self is the first argument.

This type of method can modify object state and it's usually used to perform a set of actions. On the instance variables, you can access both class and instance variables and no decorator is required to declare them. On line 31, I am calling the static method to validate the number of tables to verify if 5 is a valid number or not.

On the next line, I'm creating a brand new object called DB1 where I'm passing the number of the table's first name and the last name. Online 33, I'm calling the instance method to print the first name of the DB owner. On line 34, I'm calling the alternate constructor to create a new DB2 object.

On the last line, I'm printing the first name of the DB owner again but this time for the DB2 object. Now watch what happens if I try to pass a negative value to the static method called to validate the number of tables. I get a value error exception as expected.

There you have it. Make sure you like, subscribe, and turn on the notification bell. Until next time.

class MySQL:
def __init__(self, number_of_tables, db_owner_first_name, db_owner_last_name):

# 1. tied to the class, 2. cls is the first argument, 3. can modify class state, 4. used to create factory
# methods or alternative constructors, 5. can access class variables, 6. @classmethod decorator
@classmethod
def mysql_from_full_name(cls, number_of_tables, db_owner_full_name):
first_name = split_list[0]
last_name = split_list[1]
return cls(number_of_tables, first_name, last_name)

# 1. tied to the class, 2. no cls or self required as 1st argument, 3. can't modify class or object's state,
# 4. used to create utility methods, 5. can't access class or instance variables, 6. @staticmethod decorator
@staticmethod
def validate_number_of_tables(num):
if num [removed] 0:
raise ValueError("Invalid number of tables provided")
else:
print("valid")

# 1. tied to the object, 2. self is the first argument, 3. can modify object's state
# 4. used to perform set of actions on the instance variables, 5. can access class/instance variables
def print_db_owner_first_name(self):

MySQL.validate_number_of_tables(-1)
db1 = MySQL(5, 'john', 'smith')
db2 = MySQL.mysql_from_full_name(10, 'jane doe')
Рекомендации по теме