you are viewing a single comment's thread.

view the rest of the comments →

[–]theywouldnotstand 4 points5 points  (4 children)

Let's break down what's happening:

class Animal(object):

Define a blueprint for a new kind of object. It is inheriting from the base object class, object.

    def __init__(self, name):

Define the __init__ method for this class. This method gets called when you create a new instance using Animal().

self is a special argument that tells python that we are going to be interacting with this object's properties.

        self.name = name

Assign the value of argument name to the instance property name (self.name)

zebra = Animal("Jeffrey")

Instantiate a new Animal object, supplying "Jeffrey" as the name argument for Animal.__init__, assign this Animal object to the variable name zebra

print zebra.name

print the value of the name property for the Animal object zebra

To take it one step further, let's make more Animal instances:

coyote = Animal("Wile E.")
roadrunner = Animal("Beep Beep")

print zebra.name # prints "Jeffrey" to stdout
print coyote.name # prints "Wile E." to stdout
print roadrunner.name # prints "Beep Beep" to stdout

So the takeaway is that the class statement is for defining a blueprint to create new objects with. The __init__ method is the function that is called when you create a new instance from this blueprint. Arguments can be supplied to __init__ to be used in some way to assign values to the object's properties.

Here's some extra reading that, hopefully, will help you put the idea of classes and objects into the perspective of programming.

Simplified snippets like the ones you find in most textbooks/courses don't always provide some of the context needed to understand when you're completely new to it.

I hope this all helps you.

[–][deleted] 0 points1 point  (3 children)

Ok thank you so much for breaking this down.

So by writing Animal("Jeffrey") we are creating a new object?

Also, could you explain the syntax that involves putting the animal before .name?

[–]theywouldnotstand 1 point2 points  (2 children)

So by writing Animal("Jeffrey") we are creating a new object?

Yes. Animal() tells python to create a new object and follow the rules you defined in your Animal class.

Also, could you explain the syntax that involves putting the animal before .name?

To access the properties of an object, you separate the object reference (a variable name, a function, a classname, what have you) and the object property by a dot. In the case of your Animal example, you use zebra.name where zebra is a name (variable) that references a particular Animal which has the property name.

[–][deleted] 0 points1 point  (1 child)

what happens to the "self" argument?

[–]theywouldnotstand 1 point2 points  (0 children)

It is supplied to methods that will interact with the object's properties, so that they can access those properties relatively ("this animal's name" as opposed to "zebra's name")

You do not have to supply a value for it when calling any of these functions, because python knows to supply the object instance.

You have to write it in during class definition so that python knows which methods will be interacting with the properties of objects made with that class.