This is going to look like class inheritance but I think it is not and there must be an easy way of doing the following. Take a look at this simple code:
class Land:
def __init__(self):
print "a new Land"
self.farms = []
def addfarm(self):
self.farms.append(Farm())
class Farm:
def __init__(self):
print "a new farm"
self.animals = []
def addanimal(self,name):
self.animals.append(Animal(name))
class Animal:
def __init__(self, name):
print "hi, I am %s" % name
self.name = name
USA = Land()
USA.addfarm()
USA.farms[0].addanimal('George')
USA.farms[0].addanimal('Martin')
USA.addfarm()
USA.farms[1].addanimal('Polly')
USA.farms[1].addanimal('Ralph')
Is there an easy way of getting all animals without doing?:
for eachfarm in USA.farms:
for each in eachfarm.animals:
print each.name
I am asking this because if for instance the user wants to add a new George to farm 0 I would like to quickly be able to say that name is taken. I would also be able to quickly run a function that gives me all animals in the land or all farms. Should I be writing functions for all that or Python got its own?
I am also interested on knowing if my nested class structure is not correct and could end up causing issues.
For instance, lets say I have a function that given an animal tells me the perfect food mix for it. I would like to be able to run that function on all my animals and write back into their object. If they are nested I am afraid the function may get confused!
Thanks!
Using nested classes like this is perfectly fine and is not about inheritance at all. However you may want to choose slightly different data structures.
You say that in each farm you only want to be able to have one animal of each name. However, you use a list to store them. A list allows you to have as many animals of the same name inside as you want to, so you'd need to perform that check yourself when you add another one.
However, you could use a dict. A dict is an unordered data structure that links a key to a value. In your case you could use the name of the animal as the key and the Animal object for the value. Checking if a key exists can be done in constant time (as compared to linear time with a loop), since internally a dict is a hash table.
Example code might look like this:
class Land:
def __init__(self):
print "a new Land"
self.farms = []
def addfarm(self):
self.farms.append(Farm())
class Farm:
def __init__(self):
print "a new farm"
self.animals = {}
def addanimal(self,name):
if not name in self.animals:
self.animals[name] = Animal(name)
return True
return False
class Animal:
def __init__(self, name):
print "hi, I am %s" % name
self.name = name
USA = Land()
USA.addfarm()
USA.farms[0].addanimal('George')
USA.farms[0].addanimal('Martin')
USA.addfarm()
USA.farms[1].addanimal('Polly')
USA.farms[1].addanimal('Ralph')
This would prevent you from adding two animals of the same name to one farm, returning a boolean depending on whether the animal could be added to the farm or not.
To get all animals on all farms you will still need nested loops. But enabling iteration over the objects itself can be much nicer. If you do the following:
class Land(object):
def __init__(self):
print "a new Land"
self.farms = []
def addfarm(self):
self.farms.append(Farm())
def __iter__(self):
for farm in self.farms:
yield farm
class Farm(object):
def __init__(self):
print "a new farm"
self.animals = {}
def addanimal(self,name):
if not name in self.animals:
self.animals[name] = Animal(name)
return True
return False
def __iter__(self):
for name, animal in self.animals.iteritems():
yield animal
class Animal(object):
def __init__(self, name):
print "hi, I am %s" % name
self.name = name
You could then:
for farm in USA:
for animal in farm:
pass #do something here
According to your comment, you also want to be able to do land.getAllAnimals() and farm.getAllAnimals(). The latter is easily accomplished because farm works as an iterator over all animals. If you want a list you can simply call list(farm).
For land.getAllAnimals() there are two nice options. Both are to be added to the previous declaration.
Option 1
class Land(object):
def getAllAnimals(self):
for farm in self:
for animal in farm:
yield animal
Option 2
from itertools import chain
class Land(object):
def getAllAnimals(self):
return chain(*self)
Both will return iterators over all animals. To cast these into a list, simply call list on them. The former is easier to understand, but the latter is more concise and, in my opinion, nicer.
There is nothing wrong with nesting your loops, and it's just the way to do it. You might want to look into a more declarative approach, or you might want to store your data differently, but that's all just implementation detail and primarily a matter of taste.
Related
Good morning,
I'm trying something like this:
class Fish:
def __init__(self, name):
self.name = name
def swim(self):
print(self.name,"is swimming!")
My ourpose is to extend the class to Aquarium, which cointains a dictionary of fishes:
class Aquarium(Fish):
def __init__(self, **kwargs):
self.fishes ={}
for _, name in kwargs.items():
self.fishes[name] = Fish.__init__(self,name)
def how_many(self):
print("In acquarium there are",len(self.fishes),"fishes")
def all_swimming(self):
#???
Is it possible to implement something like Aquarium.swim() to use the method of all classes inserted? I tried it, but as result it prints out only of the last fish inserted. Any suggestion?
How can I collect many Fish() inside Aquarium()? Are there better methods?
It looks like you are confusing the idea of "is a kind of" and "contains". Writing class Aquarium(Fish) suggests that Aquarium is a kind of Fish, which it is not. An Aquarium contains fish. So, the Aquarium should not be derived from Fish.
I think this is more like your intentions:
class Fish:
def __init__(self, name):
self.name = name
def swim(self):
print(self.name, "is swimming!")
class Aquarium: # An aquarium is not a kind of fish, rather it contains fish
def __init__(self, **kwargs):
self.fishes = [] # list of all fishes in the aquarium
fishes = kwargs["fishes"]
for fish_name in fishes:
new_fish = Fish(fish_name)
self.fishes.append(new_fish) # add to your list
def how_many(self):
print("In aquarium there are " + str(len(self.fishes)) + " fishes")
def all_swimming(self):
print("The list of all fishes in the aquarium:")
for fish in self.fishes:
print(" " + fish.name)
a = Aquarium(fishes=["Nemo", "Dory"])
print(a.how_many())
a.all_swimming()
Yes, it is possible. But I think this is a better way.
class Fish:
def __init__(self, name:str):
self.name = name
def swim(self):
print(self.name,"is swimming!")
class Aquarium():
def __init__(self, fishes:Fish):
self.fishes = []
for fish in fishes:
self.fishes.append(fish)
def how_many(self):
print("In acquarium there are",len(self.fishes),"fishes")
def all_swimming(self):
for fish in self.fishes:
fish.swim()
Here is a list of suggestions that you may correct:
An aquarium is not a fish. Do not inherit from it! If you need an aspect of the Fish class then split that class and make an composite.
A dictionary is used to store a key and a value. But fish already knows that key. So why don't you use a list? Do you need the dictionary? If not use a list, it is easier to use (this is just my personal opinion).
You used **kwargs. While this is usable nobody can clearly understand what exactly you want these parameters to be. Usually it is better to use a clearly defined set of parameters.
Use typing. At least for the parameters. This is really helpful to understand your code better. Also you IDE might become more helpful if you do.
I'm trying to create three classes one mainly storing attribute of a person, the other two will access to the instance of a person and do a bunch of things to adjust the its attribute values.
I want to keep them in separate way so its easy to manage the content in each class and expand. I think this is not a inheritance problem so my code is obviously wrong for the goal but I have no clue what shall be done.
class CreatePerson():
def __init__(self):
self.to_do_list=[]
class Shop(CreatePerson):
def __init__(self,CreatePerson):
super().__init__()
def add_element(self,a):
self.to_do_list+=[a]
class Recreation(CreatePerson):
def __init__(self,CreatePerson):
super().__init__()
def add_element(self,a):
self.to_do_list+=[a]
if __name__ == '__main__':
joe=CreatePerson()
p1=Shop(joe)
p2=Recreation(joe)
p1.add_element('whole milk')
p2.add_element('reading book')
print(joe.to_do_list)
I was hoping it could return following, but obviously it didn't link
['whole milk','reading book']
If I understand correctly, you want your Shop and Recreation objects to share their underlying CreatePerson object. But your code doesn't do that. As #mike scotty commented, you pass in Joe, but don't use Joe for anything. So these new objects just create their own to_do_lists, which are not shared. Here is something like what i imagine you want:
class CreatePerson():
def __init__(self):
self.to_do_list = []
def add_element(self, a):
self.to_do_list += [a]
class Shop():
def __init__(self, CreatePerson):
self.my_person = CreatePerson
def add_element(self, a):
self.my_person.add_element(a)
class Recreation():
def __init__(self, CreatePerson):
self.my_person = CreatePerson
def add_element(self, a):
self.my_person.add_element(a)
>>> joe=CreatePerson()
>>> p1=Shop(joe)
>>> p2=Recreation(joe)
>>> p1.add_element('whole milk')
>>> p2.add_element('reading book')
>>> print(joe.to_do_list)
['whole milk', 'reading book']
So now Shop and Recreation aren't instances of a CreatePerson, but they do take a CreatePerson in their constructor, and store a reference to that person. And when you call add_element on those objects, they call the new add_element method that their CreatePerson has.
I'd just like to note that I don't think this is exactly the right abstraction - your Shop has one-and-only-one person, as does your Recreation. But I took it at face value for the purposes of answering this question.
Hope that helps, Happy Coding!
I understand how to create an unsophisticated class based on examples culled from the Web but I hit a wall when trying to access the members on it, to wit:
Let's say this is my class:
class Fruit(object):
def __init__(self, name, color, flavor):
self.name = name
self.color = color
self.flavor = flavor
def description(self):
print('I am a %s %s and my taste is %s and I am %s' % self.color, self.name, self.flavor))
To create and object I use:
lemon = Fruit('lemon', 'yellow', 'sour')
and to create a new attribute for lemon I use:
lemon.peel = 'easy'
I would like to define a method inside (or outside) of the class that will be called printall that will iterate though all the existing members of the class and print all of them with their attributes even if the attributes are variable (more than de ones initially defined). I think this is called "overloading"
but I am not sure of the proper terminology.
The term you are looking for is type introspection. Overloading is something entirely different, where you provide different implementations of a method.
You can access all instance attributes with the var() function; it returns a dictionary you can then iterate over to print your variables:
def printall(self):
for name, value in vars(self).items():
print('self.{} = {!r}'.format(name, value))
And if you're not sure then you can use the below loop to find details of all members
import gc
#garbage collector should do the trick
#all the other code
for obj in gc.get_objects():
if isinstance(obj, Fruit):
print "object name :",obj.name
printall(obj)
perhaps this is what you're looking for, though the printall method is not a part of the class, it is able to access the class when you pass an object to it and the following code should print the attribute name and value of the object lemon in Fruits class.
def printall(lemon):
for a in dir(lemon):
if not a.startswith('__') :
print a,":",getattr(lemon, a)
#rest of the code
lemon = Fruit('lemon', 'yellow', 'sour')
lemon.peel = 'easy'
printall(lemon)
I'm working in Python 2.7.8. What follows is a slight variant of the problem I'm working on.
I have a large number of custom classes that I've written where the inheritance is like a tree. The behavior is well encapsulated by the following example:
import random
class Animal(object):
def __init__(self, name):
self.name = name
self.can_own_pets = False #most Animals cannot own pets
self.get_features()
def give_pet(self, pet):
if not self.can_own_pets:
print(self.name+' cannot own a pet!')
else:
self.pets.append(pet)
def is_hungry(self):
return random.choice([True, False])
def get_features(self):
"""
In some classes, get features will be a function
that uses self.name to extract features.
In my problem, the features are extracted
with regular expressions that are determined by
by the class.
"""
pass
class Human(Animal):
def __init__(self, name):
super(Human, self).__init__(name)
self.can_own_pets = True
self.pets = []
class Dog(Animal):
def __init__(self, name):
super(Dog, self).__init__(name)
def bark(self):
print 'WOOF'
def get_features(self):
if 'chihuahua' in self.name:
self.is_annoying = True
elif 'corgi' in self.name:
self.adorable = True
My program needs to take in a large number of animals and delegate them to the correct classes -- I need the correct attributes and methods. What I would like to do is modify the Animal constructor so that if the name argument is something like "Finn the Dog" or "Jake the Human", it (the constructor) returns an instance of the class "Dog" or "Human", complete with the appropriate methods and attributes. Now, I know that I could easily write a function that takes a string and class as arguments, constructs a dictionary where the keys are the names of the subclasses of the given class, looks up the element of the dictionary that is contained in the string, and returns an object of that class. My question is whether or not there is a way to code this into the Animal class itself, which seems more elegant to me (as well as easier to maintain).
Here's an implementation --
def _get_all_subclasses(cls):
for scls in cls.__subclasses__():
yield scls
for scls in _get_all_subclasses(scls):
yield scls
class Animal(object):
#staticmethod
def from_string(s):
for cls in _get_all_subclasses(Animal):
# Somehow pick the class based on the string... This is a really simple example...
if cls.__name__ in s:
return cls()
raise ValueError('Bummer. Animal has not been discovered.')
class Dog(Animal):
pass
class Cat(Animal):
pass
class Lion(Cat):
pass
print Animal.from_string('is a Dog')
print Animal.from_string('is a Cat')
print Animal.from_string('Lions!!!')
print Animal.from_string('Lockness Monster')
There are limitations here
All of the constructors need to be pretty much the same which means that Cat.__init__ needs to basically do the same thing that Human.__init__ does.
After you create the instance, your code needs to have logic to handle Cat, Human, Dog, etc. In some cases that's Ok (e.g. the code really only cares that it is working with an Animal), but frequently it isn't (after all, Cats can walk on fences, but Humans can't).
Generally, the principle that I like to live by is to try to make the inputs to my functions permissive (is it a list or a tuple? Who cares! Duck Typing FTW!) but to try to have really well defined outputs. I think that this makes interfaces easier to use in the long haul and the code that I wrote above would probably not pass a code review if I was the reviewer :-).
To build upon mgilson's answer
You can override the __new__ method so that you can instantiate the classes like normal without a static method.
class Animal(object):
#classmethod
def _get_all_subclasses(cls):
for scls in cls.__subclasses__():
yield scls
for scls in scls._get_all_subclasses():
yield scls
def __new__(cls, name):
cls_ = cls
for subcls in Animal._get_all_subclasses():
if subcls.__name__ in name:
cls_ = subcls
break
instance = object.__new__(cls_)
if not issubclass(cls_, cls):
instance.__init__(name)
return instance
In plain english: I am creating class instances dynamically in a for loop, the class then defines a few attributes for the instance. I need to later be able to look up those values in another for loop.
Sample code:
class A:
def __init__(self, name, attr):
self.name=name
self.attr=attr
names=("a1", "a2", "a3")
x=10
for name in names:
name=A(name, x)
x += 1
...
...
...
for name in names:
print name.attr
How can I create an identifier for these instances so they can be accessed later on by "name"?
I've figured a way to get this by associating "name" with the memory location:
class A:
instances=[]
names=[]
def __init__(self, name, attr):
self.name=name
self.attr=attr
A.instances.append(self)
A.names.append(name)
names=("a1", "a2", "a3")
x=10
for name in names:
name=A(name, x)
x += 1
...
...
...
for name in names:
index=A.names.index(name)
print "name: " + name
print "att: " + str(A.instances[index].att)
This has had me scouring the web for 2 days now, and I have not been able to find an answer. Maybe I don't know how to ask the question properly, or maybe it can't be done (as many other posts seemed to be suggesting).
Now this 2nd example works, and for now I will use it. I'm just thinking there has to be an easier way than creating your own makeshift dictionary of index numbers and I'm hoping I didn't waste 2 days looking for an answer that doesn't exist. Anyone have anything?
Thanks in advance,
Andy
Update: A coworker just showed me what he thinks is the simplest way and that is to make an actual dictionary of class instances using the instance "name" as the key.
Sometimes keeping it simple is best. Having a dict that stores your instances with their names as the keys would be both straightforward and fairly simple to implement.
class A:
instances={}
def __init__(self, name, attr):
self.name=name
self.attr=attr
A.instances[name] = self
and then to get the proper instance, just...
instance = A.instances[name]
No need to put the instance dict inside the class. Just create a dict, inst in the local scope:
class A:
def __init__(self, name, attr):
self.name=name
self.attr=attr
inst={}
names=("a1", "a2", "a3")
x=10
for name in names:
inst[name]=A(name, x)
x += 1
Then, whenever you want to access a certain instance by name, just use inst[name]:
for name in names:
print inst[name].attr
Yes, the dictionary approach should work well, and can be dovetailed into the class itself.
class A:
_instances = {}
#classmethod
def get(cls, name):
return A._instances[name]
def __init__(self, name, attr):
self.name=name
self.attr=attr
A._instances[name] = self
a = A('foo', 10)
aa = A.get('foo')
If you want to play around with __new__, you can even make this transparent:
a = A('foo', 10)
aa = A('foo') # 'a' and 'aa' refer to the same instance.
This is a bit complicated, so I'll leave it to you to research (and of course ask another question on SO if you get stuck).