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?
5 Answers
+ 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
+ 1
Slick thank you!
0
Slick what do you mean "instance of a class"?
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