r/learnpython • u/ShineRayGan • Jul 10 '22
__init__ method
can some explain how does the __innit__ method works?
P.S: pls try to make the explanation as simple as possible.
20
u/Green-Sympathy-4177 Jul 11 '22
__init__ is used in Objects to initialize them
Say you have a Person object with a firstname and a lastname:
# Create a "Person" class
class Person:
# When you create a new instance, this will be called
def __init__(self, firstname, lastname):
# assign the firstname passed to this instance's firstname
self.firstname = firstname
# assign the lastname passed to this instance's lastname
self.lastname = lastname
# Just print stuff to see that it's being called
print(f"{self.firstname} {self.lastname} was initialized !")
# Create an instance of Person
john = Person(firstname="John", lastname="Doe")
>> John Doe was initialized!
print(john.lastname)
>> Doe1
u/fakemoose Jul 11 '22
Why would you do it this way instead of defining a function? That’s where I always get confused with init. It seems like extra work for the same result.
3
u/ExcellentAd9659 Jul 11 '22
Good question. What you see is a very small class. Classes are used for long scripts. If you were to make something and you had to use a class, it'd be very long, or else you can just use functions.
2
u/Green-Sympathy-4177 Jul 11 '22
Basically
__init__is a method inside an Object, nowhere else.In the example I provided the object is as basic as it gets.
But what if we change the class and give to "Person" object the ability to greet themselves? And a property to get the fullname from the firstname and lastname ?
class Person: def __init__(self, firstname, lastname): self.firstname = firstname self.lastname = lastname def greet(self): print(f"Hi, I'm {self.fullname} !") @property def fullname(self): return f"{self.firstname} {self.lastname}" alice = Person("Alice", "Pink") bob = Person("Bob", "Red") charlie = Person("Charlie", "Brown") alice.greet() >> Hi I'm Alice Pink !And now imagine you have tons of instances. Go and manage that with functions... It's doable, but it will be a mess.
There's a lot more to the whole "object vs function", google: "OOP vs function, python"
1
u/Tiktoor Jul 11 '22
What is self? It gets assigned to the related variable name?
4
u/Green-Sympathy-4177 Jul 11 '22
selfis a reference to the instance of the class, to take back the same example as before:class Person: def __init__(self, firstname, lastname): self.firstname = firstname self.lastname = lastname def greet(self): print(f"Hi, I'm {self.fullname} !") @property def fullname(self): return f"{self.firstname} {self.lastname}"We have our class
Person, when we instantiate this class, like this:alice = Person("Alice", "Pink")When we use
alice.greet(), it callsself.fullname,selfin that instance is the Person alice. If we had another onebob = Person("Bob", "Black")then when we're usingbob, thenbobisselfin that case.It's a terrible way to explain it.
Google's response:
The self parameter is a reference to the current instance of the class, and is used to access variables that belongs to the class.
Read more about Object Oriented Programming (OOP) in Python:
6
Jul 11 '22
Init is the part of the instructions used to make an object. If both your parents have black hair and long noses…. The ‘code’ to init you should say black hair, long nose. That way every time your parents have a kid, all your brothers and sisters will have this set of properties. Another example is, if you make the instructions to build a car, you can init ialize it with 4 wheels. Even if everything else changes. The cars will all have 4 wheels. Basically the part of the ‘instructions’/class That stays the same, no matter how many times you use the instructions or ‘class’ to build many objects ( your siblings or cars in the examples above)
4
u/Sir_Chester_Of_Pants Jul 10 '22
When you create a new instance of a class, the init method is called. Basically it is the initial set up that the object needs to go through, this is typically where a lot of attributes are initialized
1
u/Dr_Physics_ Jul 11 '22
init initializes your class. Here you can set variables that your class always needs or functions that your class will always run.
1
1
1
u/zaRM0s Jul 11 '22
The way I like to think of init is it’s like the default case for the program. For example, if you’re writing a program which has a car class, we can initialise that class using init dunder (double under). Each car might have 4 doors for example, we can use this dunder method to set this up by passing doors as a parameter into the init dunder. I would also look into things like repr and other dunders too because it might enlighten you to why we might use dunders. They allow a great deal of control for error messages, formatting of outputs etc.
Excuse my formatting, I am on my phone lol
0
Jul 11 '22
Just to practice, I'll try answering (a learner myself).
__init__ does what it says, it initialises the class. It "starts up" the class by laying out the values that will be used in the class
I find it helps to read "init" in full as "initialise"
32
u/publicfinance Jul 10 '22
Where should I start?