Skip to content

Instantly share code, notes, and snippets.

@iris9112
Created May 17, 2020 05:51
Show Gist options
  • Save iris9112/c462d917731913b95abffcbce493a9b7 to your computer and use it in GitHub Desktop.
Save iris9112/c462d917731913b95abffcbce493a9b7 to your computer and use it in GitHub Desktop.
# Minimal example of POO
class Person(object):
has_animal = False
pets = list()
def __init__(self, name, last_name):
self.name = name
self.last_name = last_name
def __str__(self):
return f"{self.name} {self.last_name}"
def get_pets(self):
return [pet for pet in self.pets]
def has_pets(self):
return self.has_animal
class Pet(object):
vaccines = list()
def __init__(self, animal, name, years_old, owner=None):
self.animal = animal
self.name = name
self.years_old = years_old
self.owner = owner
def __str__(self):
if self.owner:
return f"{self.name} is {self.animal} and have {self.years_old} years old. His owner is {self.owner}"
else:
return f"{self.name} is {self.animal} and have {self.years_old} years old."
def adoption(self, person):
person.pets.append(self)
person.has_animal = True
self.owner = person
return f"{self.name} adoptado por {person.name}"
def get_owner(self):
if self.owner:
return f"Su dueño es {self.owner}"
else:
return f"{self.name} no ha sido adoptado"
def add_vaccine(self, new_vaccine):
self.vaccines.append(new_vaccine)
return f"Agregada nueva vacuna: {new_vaccine}"
def allows_adoption(self):
if self.owner:
return False
else:
return True
def is_death(self):
pass
# create person instances
isa = Person(name="isabel", last_name="Ruiz Buritica")
print(isa)
felipe = Person(name="Andres Felipe", last_name="Ruiz")
print(felipe)
hector = Person(name="Hector", last_name="vega")
print(hector)
oscar = Person(name="oscar", last_name="Barajas")
print(oscar)
# create pets instances
percy = Pet(animal="gato", name="percy", years_old="3")
print(percy)
elmo = Pet(animal="perro", name="Elmo", years_old="1")
print(elmo)
tomas = Pet(animal="perro", name="Tomas", years_old="5")
print(tomas)
Callejera = Pet(animal="gato", name="Callejera", years_old="1")
print(Callejera)
# vaccine pets
print(percy.add_vaccine("triple felina"))
print(elmo.add_vaccine("parvo virosis"))
print(tomas.add_vaccine("pervo virosis"))
# adopted pets
print(percy.adoption(isa))
print(tomas.adoption(felipe))
print(elmo.adoption(oscar))
# validated methods for pets
print(elmo.get_owner())
print("Elmo puede ser adoptado? ", elmo.allows_adoption())
print(Callejera.get_owner())
print("La callejera puede ser adoptada? ", Callejera.allows_adoption())
# validated methods for person
oscar.get_pets()
print(oscar.has_pets())
hector.get_pets()
print(hector.has_pets())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment