5. Instantiating your first object question



class Animal(object):
def init(self, name):
self.name = name

zebra = Animal("Jeffrey")
print zebra.name

Hello, I was able to finish the exercise correctly, but can someone explain to me why putting 'Jeffrey' in the "object" parameter will pass the object as the name?

Basically, I'm not sure how the computer knows that the object argument is specifically designated for the name attribute. Like what if the class had multiple attributes?

Thanks for the help

Replace this line with your code.


Take a look at this example:

class Point3D(object):
    def __init__(self, x, y, z):
        self.x = x
        self.y = y
        self.z = z

my_point = Point3D(10, 20, 30)
print my_point.x, my_point.y, my_point.z
=> 10 20 30

When we create a new object (Point3D(10, 20, 30)) method __init__ is executed. In Point3D class this method takes self and three additional arguments.

What decides that 10 is referenced as x and 20 as y? Order of passed values.

First argument that __init__ takes is x and 10 is the first passed parameter (Point3D(10, 20, 30)).


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.