r/learnpython • u/dkirk500 • Jul 06 '20
What's an __init__() method and why we need it?
Am new to python. Will appreciate if someone can explain in a very basic way with example, what and why we need an init () method? What will happen if we don't use one?
2
8
u/11b403a7 randall-s-lee Jul 06 '20
It's the constructor for your class
3
u/mongoloid404_af Jul 06 '20
RP is a great resource to learn Python.
Check out this blog Object-Oriented Programming (OOP) in Python 3
2
u/ReflectedImage Jul 06 '20
If you skip init your code will appear to be correct but be subtly wrong, consider:
class Car:
model = "Toyota"
my_car = Car()
my_second_car = Car()
my_second_car.model = "BMW"
print("Car models:")
print(my_car.model) # Toyota
print(my_second_car.model) # BMW
del my_second_car.model
print("Car models:")
print(my_car.model) # Toyota
print(my_second_car.model) # ToyotaOutput:
Car models:
Toyota
BMW
Car models:
Toyota
ToyotaThe code appears to work at the beginning with it displaying Toyota and BMW for the two cars. However when we deleted the my_second_car.model attribute you would expect my_second_car to be undefined and raise an exception but instead it returns Toyota. This is because there are class attributes and instance attributes since we skipped the init function, model = "Toyota" is a class attribute, we can override it with my_second_car.model = "BMW" and it appears to work since my_second_car now has the class attribute and an instance attribute that overrides the returned value, but we can then just delete the instance attribute and get back the class attribute seemingly from no where.
The short answer is if you don't use init you will get some wacky and hard to debug problem somewhere down the line.
1
u/Gl1tch54 Aug 11 '20
So, if I understood correctly, if you used an Init, deleting the attribute of an instance would empty it, raising an exception when the code runs and letting you know something isn't right
But when you change a class attribute with an instance attribute, and then delete the instance attribute, the object will reset its attribute to the class one and the program will fail unexpectedly since you programmed with the instance attribute in mind
Right?
2
u/CrambleSquash Flask, Pandas, Numpy Jul 06 '20 edited Jul 06 '20
To give a slightly more complete answer.
Python is a bit strange in that if I have a class:
class CarType:
def __new__(cls):
instance = super().__new__(cls) # this does what __new__ does by default
print("I made a new instance:", instance)
print(instance.__dict__)
return instance
def __init__(self):
print(self, "is getting initialised")
self.fuel = 100.0
print(self.__dict__)and then I make an instance:
>>> car_instance = CarType()
I made a new instance: <__main__.CarType object at X>
{}
<__main__.CarType object at X> is getting initialised
{'fuel': 100.0}As you can see, getting my_instance ready to go takes place in 2 steps. (arguably 3).
super().__new__(cls)(i.e. what usually happens if you don't define your own__new__) creates an instance of your class. This means it finds a spot in memory to put your instance. However, as you can see, at this point the object has no attributes (these are stored in the 'special'__dict__attribute of your instance).Your
__init__method is then called, where the instance that was made before is passed as the first argument (you can tell this because the locations in memory are the same), which we usually callself. At this point I can add attributes to this instance as I please, and these are put into theinstance.__dict__.
In the vast majority to cases we just ignore __new__ and just define an __init__. Because the instances made by __new__ are basically blank slates, __init__'s role is to add all the attributes and possibly other setup needed for your instance to 'make sense', this means in general all attributes should be defined in your __init__, adding them outside of __init__ is naughty! In other words, if I make a Car class, when I make a Car instance, cars have a fuel level as soon as they exist, so fuel level should be an inttribute in the __init__.
What's the point of __new__ then? In short it's for when you want to be fancy. Writing a custom __new__ is used when you want to control what blank slate your user gets. For example in Pandas, when you create an index:
>>> index = pd.Index(range(10))You'll find what you get out isn't necessarily what expect:
>>> type(index) == pd.Index
False
>>> type(index)
RangeIndex(start=0, stop=10, step=1)In Index.__new__ pandas looks at the arguments you've supplied and decides that it knows what class you want better than you do, so in this case it thinks, you don't want a plain old Index, this is a range, so it creates, initialises and returns a RangeIndex instance instead.
1
u/kingbradley1297 Jul 06 '20
As a follow up question, are destructors also a thing in Python? I've learnt it in C++ but wanted to know if it's used here
2
Jul 06 '20 edited Jul 06 '20
[removed] — view removed comment
1
u/kingbradley1297 Jul 06 '20
Oh I see. So the pointer just stops pointing to the object once it goes out of scope from what I understand. But then how does garbage collection occur?
3
u/callmelucky py34int Jul 06 '20 edited Jul 06 '20
Just FYI, generally "dunder" methods (like
__methodname__) are called as if they are global functions, so you would dodel(my_class_instance)to use this.*Garbage collection is handled automatically in Python, you don't need to worry about it. But to answer your question directly, I don't know how it occurs.
*Edit: according to this comment below, what I said about dunder methods is generally/often true, but is not necessarily true for
del()vs__del__specifically.2
u/nog642 Jul 06 '20
Many dunder methods have associated global functions, but not
__del__.
delis a keyword, not a function, and it does not necessarily call the__del__method, it just deletes a variable, which is a reference to an object.2
2
u/nog642 Jul 06 '20
From what I understand, Python uses reference counting, and when there are 0 references to an object, it gets garbage collected (and
__del__is called). On top of that, I think there is another garbage collector that runs periodically to collect everything that reference counting wouldn't catch (like reference cycles). You can Google about the Python garbage collector; there is lots of information online.1
1
Jul 06 '20
I see that you have gotten almost identical, but unfortunately not 100% correct answers, Dunder init is not the constructor. It's the initializer. The constructor is dunder new.
1
u/nog642 Jul 06 '20
No, the constructor is not necessarily the function that creates the object.
__init__is far more analogous to the constructor in languages like Java and C# than__new__.The constructor just initializes the object; it doesn't return anything.
__init__just initializes the object; it doesn't return anything.__new__returns the object, and does no initialization usually.1
Jul 06 '20
You are wrong. Stop spreading misinformation. Read up on metaclasses instead, and come back to apologize when you've done so.
2
1
u/nog642 Jul 07 '20
I know how metaclasses work. I don't really see how that's relevant to what exactly you call the constructor.
0
Jul 07 '20 edited Jul 07 '20
Says there are no constructors in Python
Claims knowledge of the very thing that allows for customizing said constructors.
Skill level: Expert beginner.
34
Jul 06 '20
[removed] — view removed comment
7
u/blackuchiha1192 Jul 06 '20
This is a really good description. It actually just helped me better understand classes haha
3
u/constableVisit Jul 06 '20
Why does the 'self' need to be a parameter?
4
u/NerdEnPose Jul 06 '20
Self references the object that is being initialized. There's a bit Python does under the hood here but the first variable passed into methods is always the object. For example:
t1 = Rectangle(10,5)
In this case self will reference t1. (This is an over simplification but it helps)
t2 = Rectangle(7,3)
Now self references t2.
That way when you call:
t1.height # 10
t2.height # 7
By using self, you store the height and width to the proper object.
3
u/toastedstapler import itertools Jul 06 '20
because
obj.method(*args)is effectivelyclass.method(object, *args). run this code below and they'll behave identically. python sees that you're calling an instance method on an instance and auto fills in the instance asselfclass A: def method(sef, a): return a a = A() a.method(4) A.method(a, 4)1
1
u/pblokhout Jul 06 '20 edited Jul 06 '20
Imagine we want to make "dead" classes (static classes) and "alive" classes (instantiated classes).
When you write a class you're really describing a blueprint for behavior. Let's say we program a cat. When we say "lick your butt" a cat is only supposed to lick its own butt (self), not the butt of all (instantiated) cats that came from the same blueprint. On the other hand, when we keep a (static) record of all cats the record is not necessarily alive. It has no behavior or unique identifier of its own. We might keep a record in different places but whenever we change it somewhere, we want the changes to take place everywhere. Otherwise we have different records that don't match. In other words, the cats have a self. A unique property that makes them different from other cats even though they come from the same blueprint. The cat is not the same thing as its blueprint.
With a record of cats we want the record to be exactly the same everywhere, so the blueprint is the record.
4
u/zwitter-ion Jul 06 '20 edited Jul 06 '20
Whenever you declare an instance of a class, the
__init__()method is always executed first if its present. You can use this to executes code that "constructs" the class by defining class variables, or anything really to be able to use the class further.For example if you have a class for cars, when you create a car object you might want to initialize some things like the name, model, year, etc. while creating the object. You use the
__init__()method to do that.You need not define a
__init__()but its a common practice to use it to setup the class variables or the initialization part.