0

constructors python

why do you write 'self' in the argument at a function that prints the data? like this function: def printInfo (self): print ("name: " + self.name + " , age: " + str(self.age)) why is that neccesary here? is 'self' like 'this' in java?

7th Jul 2020, 10:12 AM
Yahel
Yahel - avatar
5 Answers
7th Jul 2020, 10:15 AM
ŰȘŰ±ÙƒÙŠ Ù…Ù†Ű”ÙˆŰ± Ű§Ù„Ű­Ù…ÙŠŰ§Ù†ÙŠ
 ŰȘŰ±ÙƒÙŠ Ù…Ù†Ű”ÙˆŰ± Ű§Ù„Ű­Ù…ÙŠŰ§Ù†ÙŠ  - avatar
+ 1
Thats in OOP. "self" refers to an instance of a class. class Car: def __init__(self, make, model, year): self.make = make self.model = model self.year = year my_car = Car('Ford', 'F150', '1973') print(my_car.model) #F150
7th Jul 2020, 10:19 AM
Slick
Slick - avatar
+ 1
Slick thank you!
7th Jul 2020, 10:32 AM
Yahel
Yahel - avatar
0
Slick what do you mean "instance of a class"?
7th Jul 2020, 10:25 AM
Yahel
Yahel - avatar
0
If we think of a class as a blueprint of an object, then we think of the instance of the class as the actual object! Each Car object thats created is an instance of the Car class
7th Jul 2020, 10:28 AM
Slick
Slick - avatar