python - Link two classes together by variables -
i learning python couple different books learn python hard way.
i trying better understand classes , ran example :
class dog(object): def __init__(self, name): self.name = name self.legs = 4 self.carry_weight = 0 class person(object): def __init__(self,name): self.name = name self.pet = none self.weight = 0 rover = dog("rover") rover.carry_weight = 180 frank.pet = rover print frank.name print frank.pet.legs if frank.weight < frank.pet.carry_weight: print "yes, frank can ride dog %s around neighborhood" % frank.pet.name
so learned can link 2 instances of class , call attributes 1 other. wanted add attribute
owner = none
under dog. , when link frank rover links rover frank owner if know rover can pull frank.
what have come idea:
def assign_pet(self, petsname): self.pet = petsname petsname.owner = self.name
this seems work fine wanted know if best way go this. there better way?
i don't know programs , don't have anywhere go questions. tried search answer feel 1 of 3 blind men feeling elephant, 1 moment im googling "whats rope thing hangs 7 feet off ground" , next i'm googling "whats tree trunk stomped on face". hope guys understood joke. hard search can't describe properly.
Comments
Post a Comment