What is the problem with it?


#1

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

#2

creating an instance of your class:

    hippo = Animal("as" , 3)
    hippo.description()

should be outside your class deceleration (hint: indention, or rather, no indention for this lines)


#3

I tried to remove indention but its not working ..


#4

can i see an updated version of your code?


#5

wait a minute...

isnt

supposed to be hippo.description?
cause i wrote .description without () when i did it last time and i got it...


#6

no, to call methods parentheses are required. since you might need to supply arguments:

class example_class:
   member_variable = "see"
   def example_method(self, example_argument):
      print example_argument
example = example_class()
example.example_method("hello world")
example.member_variable

without parentheses is for member variables.


#7

@stetim94, it worked for me without the () for some reason?


#8

true, but look:

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

see that then prints that this is a method of the animal object and the memory address of the object. not really nice, while if you do:

print hippo.description()

you get the name and age, instead of the method object.

this is the same with functions:

def example():
   print "hello world"
print exampe # will print the function and the memory address
print example() # will print hello world

so yes, it can, but the output given is not really nice.


#9

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