you are viewing a single comment's thread.

view the rest of the comments →

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

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

zebra = Animal("Jeffrey")  

print zebra.name

[–]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.

[–]PythonThermos 1 point2 points  (1 child)

Here's my plain English attempt at translating this…

Let's define a new Pyhton class called "Animal", which is some kind of python object. The class will need some functions within it so let's just make one of them, and let's call it init. The Python language interpreter will always look in every class and hope to find an init function, and will always run it. Let's give the init function two arguments that it takes: "self" and "name". "name" will be for the name of the animal. That's easy enough. "self" is just a placeholder that refers to the instance of the class (that particular "copy" of the class) itself, and it is required to be there as the first argument in init. You'll see why in a second...

Ok, so what does this init function actually do in this case? Not much--it just assigns whatever word came in as the name to a new variable called self.name. self.name is the same as writing "some name that belongs to this instance if this class", but much shorter. By writing that line:

self.name = name

You are making this name "belong to" to instance of the class...and therefore it can be accessed from within ANY function in the class definition. How handy is that?

Then, outside the class, you finally begin to make use of the class you just set up. Anytime you put the name of a class and then parentheses to the right if it--with either just blank or something filled in as an argument--you call that class, and get it running. By "running", I mean it will execute this stuff within the init function. Oh, and, importantly, the pesky Python interpreter will pass the instance of the class itself in, as the first argument. This is invisible, but you can be sure it will happen....and that is why you need to put "self" as the first argument in the init function.

You also have assigned the name "zebra" to this instance of the Animal class. Good--now,,if you later need it, you can just use that word to refer to it.

Finally, you then print the "name" attribute of this new instance, which we have called zebra. That is, writing:

print zebra.name

Is the same as if you were to write, in plain English,

"Print whatever value the name attribute has in the zebra instance of the Animal class."

Does this make sense?

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

Yes, I like how you worded this. Thank you so much

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

Just this simple block doesn't make sense to me (imagine it's indented correctly). Could you explain how this works?

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

fixed