9/18 - calling class methods


#1

why are some methods written

> print hippo.health

and others

> hippo.description()

and yet others

> print xxx.yyy(hippo)

meaning, what is the significance of the parentheses?


Bug in lesson 8
9/18 Ils se multiplient!
#2

The parentheses let python know you are calling a function and not a variable. If the function takes parameters, then you would pass the parameters to the function by placing something inside the parentheses.

If you do

print hippo.health

It will return the value of the variable health. health is not a function but a variable, which is an important distinction to make!


#3

This is my code (french version)

class Animal(object):
"""Fait des animaux mignons."""
sante = "bonne"

def __init__(self, nom, age):
    self.nom = nom
    self.age = age

def description(self):
    print self.nom
    print self.age
    print self.sante

hippo = Animal("john", 35)
paresseux = Animal("dodo", 12)
ocelot = Animal("chat", 2)

print hippo.sante
print paresseux.sante
print ocelot.sante

I don't understand why it doesn't work.


#4

not sure why this extra line is here. you don't need it here.

> class Animal(object):
>     """Makes cute animals."""
>     is_alive = True
>     health = "good"
>     def __init__(self, name, age):
>         self.name = name
>         self.age = age
>     # Add your method here!
>     def description(self):
>         print self.name
>         print self.age

> hippo = Animal("larry",3)
> sloth = Animal("jim", 5)
> ocelot = Animal("dick", 9)

> print hippo.health
> print sloth.health
> print ocelot.health

#5

This extra line isn't important.
the error message is :

Oops, try again.
Votre code semble un peu Ă©trange. Jetez un oeil Ă  l'indice si besoin!

translate :

your code seems a little strange, lookthe clue if you need!

but the clue is useless in my case


#6

I'm not sure.
The only thing I can recommend is making sure that all of your indenting is correct.
Sorry I couldn't be of more help.


#7

Try to call the function without the print statement, since it is already written in the function. You are double printing!


#9

J'ai exactement le mĂŞme bug

class Animal(object):
    """Fait des animaux mignons."""
    est_vivant = True
    sante = "bonne"
    
    def __init__(self, nom, age):
        self.nom = nom
        self.age = age
    
    # Ajoutez votre méthode ici!
    def description(self):
        print self.nom
        print self.age
    
hippo = Animal("potam",2)
paresseux = Animal("aaaa",1)
ocelot = Animal("bbbbb",2)

print hippo.sante
print paresseux.sante
print ocelot.sante

Cela répond à la lettre au challenge et affiche les bonnes réponses.
Les bugs sur la version française sont courants. J'avais déjà terminé la version anglaise qui est mieux faite.
En attente de l'examination par un admin de codecademy...


#10

I did this

print hipopotamo.nombre, hipopotamo.salud
print perezoso.nombre, perezoso.salud
print ocelote.nombre, ocelote.salud


#11

Some have it right. The tricky part for me was the class variable indentation. Its part of the class definition so even if its at the end of the class script, you need to indent it as part of the class. Here is what worked for me. Its a real test to see if we understand the differenece between member variables and class variables. My (working) code on Code Academy AND on a Python Editor is as follows:

class Animal(object):
    """Makes cute animals."""
    is_alive = True
    health = "good"
    def __init__(self, name, age):
        self.name = name
        self.age = age
        
    # Add your method here!
    def description(hippo):
        print hippo.name
        print hippo.age
        
hippo = Animal("Paul", "42")
sloth = Animal("Bob", 33)
ocelot = Animal("Leo", 44)
print hippo.health
print sloth.health
print ocelot.health

#12

Same problem for me in French version.

I try to add this to my code but doesnt work too...

Exception = "somebody should get around to fixing this..."


#13

Le problème de la version française : elle n'a pas été traduite pas le site CodeAcademy, mais par une association française qui s'est chargé de cela, du coup les bugs sont vraiment nombreux, comme les fautes de traduction à certains endroits.

Malheureusement c'est Ă©galement le cas ici !


#14

Do you?

I certainly don't...


#15

@sean_raven Class Variables are variables for use with the actual class (i.e. the variables are made available to the class they are in only and declared directly under the class definition) where as member variables are variables (aka instance variables) which are declared in your init() method and for the instance of the class only. These member/instance variables are made available to every member of the instance of the class. This type of variable applies to OOP related design patterns in Python and they can be inherited by (sort of) sub (other classes) classes that reference the class object they are declared as instance variable in. Finally, local variables can be declared in and for individual functions/methods depending on how you are organising your project For more on variables, please see a nice article on it at: http://pythoncentral.io/introduction-to-python-classes/


#16

so, only member vars can be inherited by subclasses?


#17

Yes, but you can call class variables directly through a classname.valvariable call. This makes them callable directly from other classes but like static variables in Java, C#, they perform a slightly different function (in practice) to instance/member variables.


#18

Hello, same issue.
I found a tricks here : https://discuss.codecademy.com/t/cours-ils-se-multiplient-9-18/2131
Add this before your code :
def aattr(a, b):
return True


#19


#21

met ceci a la fin du code:
def x():
.... return True