class - python multiple nested classes -
this going class inheritance think not , there must easy way of doing following. take @ 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, %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 easy way of getting animals without doing?:
for eachfarm in usa.farms: each in eachfarm.animals: print each.name i asking because if instance user wants add new george farm 0 able name taken. able run function gives me animals in land or farms. should writing functions or python got own?
i interested on knowing if nested class structure not correct , end causing issues.
for instance, lets have function given animal tells me perfect food mix it. able run function on animals , write object. if nested afraid function may confused!
thanks!
using nested classes fine , not inheritance @ all. may want choose different data structures.
you in each farm want able have 1 animal of each name. however, use list store them. list allows have many animals of same name inside want to, you'd need perform check when add one.
however, use dict. dict unordered data structure links key value. in case use name of animal key , animal object value. checking if key exists can done in constant time (as compared linear time loop), since internally dict hash table.
example code might 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, %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 prevent adding 2 animals of same name 1 farm, returning boolean depending on whether animal added farm or not.
to animals on farms still need nested loops. enabling iteration on objects can nicer. if following:
class land(object): def __init__(self): print "a new land" self.farms = [] def addfarm(self): self.farms.append(farm()) def __iter__(self): 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): name, animal in self.animals.iteritems(): yield animal class animal(object): def __init__(self, name): print "hi, %s" % name self.name = name you then:
for farm in usa: animal in farm: pass #do here according comment, want able land.getallanimals() , farm.getallanimals(). latter accomplished because farm works iterator on animals. if want list can call list(farm).
land.getallanimals() there 2 nice options. both added previous declaration.
class land(object): def getallanimals(self): farm in self: animal in farm: yield animal option 2 from itertools import chain class land(object): def getallanimals(self): return chain(*self) both return iterators on animals. cast these list, call list on them. former easier understand, latter more concise and, in opinion, nicer.
Comments
Post a Comment