0

I'm starting to learn Python from Learn Python The Hard way by Zed A. Shaw. But I'm having a hard time to understand what init function in a class does and what is self? Please help me understand this.

  • A class is sort of like a blueprint. Say you have a car class, the class is the blueprint to build the car. `__init__` is the initializer which is called when an object, or instance of a car is created. So, the class tells you how to build it and what it will have (x windows and y seats) and the object is an instance of the car itself. `self` is just a name (can be anything else) that refers to the current instance, or itself – Andrew Li Jan 02 '17 at 05:06
  • Possible duplicate of [Python \_\_init\_\_ and self what do they do?](http://stackoverflow.com/questions/625083/python-init-and-self-what-do-they-do) – pzp Jan 02 '17 at 05:09
  • So the properties of a class (say car) like color , number of wheels should be defined in the __init__ function? Right? – samacker77so Jan 02 '17 at 05:10

1 Answers1

0

___init____ initializes an instance of a class or an object.

Refer these links for more info:

Why do we use __init__ in python classes?

What is the purpose of self?

Hope this helps.

Edit

Like this:

class Car:
    def __init__(self, Wheels, colour):
        self.Wheels = Wheels
        self.colour = colour

x = Car(4, "Red")
y = Car(4, "White")
Community
  • 1
  • 1
Ambrish Pathak
  • 3,813
  • 2
  • 15
  • 30