JaeGeeTee
JaeGeeTee

Reputation: 523

Constructing Python Class

I'm new to python and a total noob with programming so bear with.

If I was building show Person class

    class Person:
      def __init__ (self, Name, Address, Phone, Height, Weight):
        self.name = Name
        self. Address = Address
        self.Phone = Phone
        self.Height = Height
        self.Weight = Weight
        self.PoundserPerInch = int(Height) / int(Weight)

what exactly does the __init__() function do for the code?

Upvotes: 1

Views: 1152

Answers (1)

Simeon Visser
Simeon Visser

Reputation: 122376

The __init__ method is the constructor. It contains code that is run for each object that is created from that class. It contains code to initialize the object in a proper state.

In your case, you have a Person class which can be used to create Person objects. If you would write:

p = Person('John', '10 Foo Street', '1234567890', 70, 180)

it will create a Person object with those initial values. The constructor takes the values that you passed and it'll assign them to the variables of that object. It will also compute a value called PoundserPerInch.

Upvotes: 5

Related Questions