I have a base class:
class Animal(object):
def __init__(self, name=None, food=None):
self.name = name
self.food = food
def eat(self):
print "The %s eats some %s" % (self.name, self.food)
And a class that extends it:
class Pet(Animal):
def pet(self):
print "You pet the %s." % self.name
def feed(self):
print "You put some %s into the bowl." % self.food
self.eat()
Let's say I have an Animal object, created thusly:
>>> fluffy_as_animal = Animal('dog', 'kibbles')
Is there a straightforward way in python to create a Pet object from fluffy_as_animal, short of actually writing out:
>>>> fluffy_as_pet = Pet(fluffy_as_animal.name, fluffy_as_animal.food)
Update
The reason I'm doing this implementation is that I have a class with a function that returns a set of objects of type Animal all which I want to actually use as Pet objects:
class AnimalStore(object):
def fetch(count, query=None, server=None, *args, **kwargs):
connection = connect_to_server(server)
resp = connection.fetch_animals(query)
objects = parse_response(resp)
animals = []
for a in objects[:count]:
animals.append(Animal(a['name'], a.['food']))
return animals
class PetStore(AnimalStore):
def fetch(count, query=None, server=None, *args, **kwargs):
query = 'type=pet AND %s' % query
animals = super(PetFactory, self).fetch(count, query, server, *args, **kwargs)
pets = []
for animal in animals:
pets.append(magic_function(animal))
return pets
AnimalStore and PetStore are in separate modules. AnimalStore is part of a standard Animal API and doesn't change. Basically I want to extend the Animal API by fetching a list of pets using the same mechanism used for animals, and I want to be able to take advantage of all of the other built-in functions that the Animal API already provides for its Animal and AnimalStore classes.
Petfrom the beginning? Or. Why aren't you using Delegation between Animal (the object) and it's role as a Pet?