8/18 - It prints.. but how?


#1



My problem is not about passing, i just want to know how it still works when name is not defined.


class Animal(object):
    """Makes cute animals."""
    is_alive = True
    # def __init__(self, name, age):
     #  self.name = name
     #  self.age = age            !!So i erased theese 3 rows!!
    def description(x):
        print x.name
        print x.age
    description(hippo)
hippo = Animal("asd", 5)


The thing i don't understand is it still prints "asd 5" after i erased 3 rows where name and age is defined.
So how does python knows about meaning of x.name when there is no "self.name = name" or init row and they are not defined?


#2

init is constructor for your animal class, yet you created method inside animal class, and you are giving arguments of name and age when calling, if you don't create constructor, the default one is created, that's why you can create instances


#3

So the default constructor for Animal class is set as name and age?
Because when i change print x.name with print x.something it does not works.


#4

no, default constructor is just created so you can make objects, your method holds the name and age


#5

My method can hold name and age but it can't hold sth and age? why is that? What is special with the word "name"?
When i write hippo = Animal("asd", 5) how does python knows "asd" is name?
**remember i erased def init(self, name, age) row. So how can Animal has an attribute called "name" after i erased it, it shouldn't have.


#6

there is nothing special about name variable, my guess is, you just can't pass exercise with sth variable, since instructions are to create name variable


#7

Hey,
I wondered why I need the part:

description(hippo)

under the function.

Without this part i dont get a print statement but i still can pass and start the next lessen. (I think that this should not happen.)

Why is this part important for the print statement?