What is self and __init__ in Python?
We will try to explain all this in this post. Before we explain about classes, let us make some assumptions. Imagine a scenario where you are tracking your household expenses. In partcular, milk, egg and newspaper expenses. There are 2 kinds of milk available. Let's call them blue and green. We have 2 kinds of egg available, small and large and 2 kinds of newspaper available Deccan Herald and TOI
class Expenses:
pass
print(Expenses)
class __main__.Expenses
We just created a class called Expenses. The pass just tells you that we will revisit Expenses class later. Now, let's assume, I wanted to track my expenses. How do I use the Expenses class for my purpose? I need to create an instance of a class for myself. Let's do that below.
Rohit_expenses = Expenses()
print(Rohit_expenses)
__main__.Expenses object at 0x7f74483de950>
We can see that an Expenses object is created. Rohit_expenses is an object which is an instance of the class Expenses. We cannot do much with an empty class. So let's create some variables within this class.
class Expenses:
num_days = 31
blue_milk_rate = 18
green_milk_rate = 21
deccan_herald_rate = 6
toi_rate = 5
small_egg_rate= 8
large_egg_rate = 12
We have defined number of days in a month as 31, defined different milk rates, newspaper rates and egg rates. Let's not forget that we are trying to track household expenses for any person. If I need to track my expenses, I need to give the class some initial set of information. This is where __init__ method is required to be defined in the class. A method is a function defined within a class.
class Expenses:
num_days = 31
blue_milk_rate = 18
green_milk_rate = 21
deccan_herald_rate = 6
toi_rate = 5
small_egg_rate= 8
large_egg_rate = 12
def __init__(self,name,milk_type,newspaper_type,egg_type):
self.name = name
self.milk_type= milk_type
self.newspaper_type = newspaper_type
self.egg_type = egg_type
self represents an instance of the class. When you create an instance of a class, if an __init__ method is defined, correponding variables for the instance are initialized. In this case, we are initializing the name of the person, the type of milk, newspaper, egg being purchased for that person. Let's see it in action.
Rohit_expenses = Expenses(name = "Rohit Rudrapatna" , milk_type = "green", newspaper_type = "deccan", egg_type = "small")
Neil_expenses = Expenses(name = "Neil Neller" , milk_type = "blue", newspaper_type = "toi", egg_type = "large")
To see if the __init__ method worked, let's try to access the variables attached to Rohit_expenses.
Rohit_expenses.name, Rohit_expenses.milk_type, Rohit_expenses.newspaper_type, Rohit_expenses.egg_type
---> ('Rohit Rudrapatna', 'green', 'deccan', 'small')
Worked like a charm!! What happened to those variables that we defined for the class Expenses. Are those variables created for the instance Rohit_expenses? We can find out now.
Rohit_expenses.num_days, Rohit_expenses.blue_milk_rate, Rohit_expenses.green_milk_rate, Rohit_expenses.small_egg_rate, Rohit_expenses.large_egg_rate, Rohit_expenses.deccan_herald_rate, Rohit_expenses.toi_rate
--->(31, 18, 21, 8, 12, 6, 5)
How different are those variables for Neil? Let's find out.
Neil_expenses.num_days, Neil_expenses.blue_milk_rate, Neil_expenses.green_milk_rate, Neil_expenses.small_egg_rate, Neil_expenses.large_egg_rate, Neil_expenses.deccan_herald_rate, Neil_expenses.toi_rate
--->(31, 18, 21, 8, 12, 6, 5)
The values are exactly the same for both Rohit and Neil. Variables like num_days , blue_milk_rate, toi_rate, small_egg_rate are called class variables. We call variables like name, milk_type, newspaper_type, egg_type as instance variables. What is the difference between them?
Understand that milk rates , newspaper rates , number of days in a month will be same across different instances of class Expenses. Milk rates nor newspaper rates cannot be specific to a person right? So we call such variables as class variables. Name of a person is specific to an instance of a class. Similarly, any person is free to choose the kind of milk, egg and newspaper. It becomes specific to that person or technically an instance of the class. We call such variables as instance variables. These variables are bound to the instance.
We will continue to develop these ideas in the next post. That's it for now
Comments
Post a Comment