Python syntax


#1
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('Anderson', 36)
sloth = Animal('Dale', 15)
ocelot = Animal('Fuzzy', 7)

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

whats the difference between the code above and the code below


class Animal(object):
  """Makes cute animals."""
  is_alive = True
  health = "good"
  hippo = Animal('Anderson', 36)
  sloth = Animal('Dale', 15)
  ocelot = Animal('Fuzzy', 7)

  def __init__(self, name, age):
    self.name = name
    self.age = age
  # Add your method here!
  def description(self):
    print self.name
    print self.age
    
print hippo.health
print sloth.health
print ocelot.health

#2

the bottom code creates the class instance inside the class, why would you do that?


#3

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