pythondictionaryhas-many

Has-many relationship in classes


I'm trying to get an idea of has-many relationship in python classes, although theoretically I understood the concept but when I try to implement it, I'm not getting correct output:

Code:

## Animal is-a object
class Animal(object):
    def sound(str):
        print("sound of animals")
## Dog is-a Animal, Dag has a name
class Dog(Animal):
    """docstring for Dog."""
    def __init__(self, name):
        #super(Dog, self).__init__()
        #initializing dog name
        self.name = name
    def sound(str):
        print("Dog barks")

## Cat is-a animal, Cat also has a name
class Cat(Animal):
    """docstring for Cat."""

    def __init__(self, name):
        # initializing cat name
        self.name  = name
    def sound(str):
        print("Cat meows")

    ## Person has-a pet of some kind
## Person is-a object
class Person(object):
    """docstring for Person."""

    def __init__(self, name):
        #super(Person, self).__init__()
        self.name  = name
        ## Person has-a pet of some kind
        self.pet = None
## rover is-a dog
rover = Dog("Rover")
rover.sound()
## Satan is-a cat
satan = Cat("Satan")
tiger = Cat("tiger")
satan.sound()
## Mary is-a Person
Mary = Person("Mary")
Mary.pets = [satan ,tiger , rover ] # list of pets
Mary.pets ={'cats' : [satan, tiger] , 'dogs': [rover]} #dictionary of lists of pets
print("No. of pets mary has ")
for x,y in Mary.pets.items(): 
    print(x, ":" , y)

Is this correct syntax for getting all dictionary values from a object:

for x,y in Mary.pets.items(): 
    print(x, ":" , y)

I'm getting this output:

Dog barks
Cat meows
No of pets mary has
cats : [<__main__.Cat object at 0x011F0D30>, <__main__.Cat object at 0x011F0CF0>]
dogs : [<__main__.Dog object at 0x011F0CD0>]

Why am i getting address rather than value?


Solution

  • See below

    The name property is kept on the base class. The base class implements repr as well

    ## Animal is-a object
    class Animal(object):
        def __init__(self,name):
            self.name = name
        def sound(str):
            print("sound of animals")
    
        def __repr__(self):
            return self.name
    
    ## Dog is-a Animal, Dag has a name
    class Dog(Animal):
        """docstring for Dog."""
        def __init__(self, name):
            super(Dog, self).__init__(name)
    
        def sound(str):
            print("Dog barks")
    
    ## Cat is-a animal, Cat also has a name
    class Cat(Animal):
        """docstring for Cat."""
    
        def __init__(self, name):
            super(Cat, self).__init__(name)
    
        def sound(str):
            print("Cat meows")
    
        ## Person has-a pet of some kind
    ## Person is-a object
    class Person(object):
        """docstring for Person."""
    
        def __init__(self, name):
            #super(Person, self).__init__()
            self.name  = name
            ## Person has-a pet of some kind
            self.pet = None
    ## rover is-a dog
    rover = Dog("Rover")
    rover.sound()
    ## Satan is-a cat
    satan = Cat("Satan")
    tiger = Cat("tiger")
    satan.sound()
    ## Mary is-a Person
    Mary = Person("Mary")
    Mary.pets = [satan ,tiger , rover ] # list of pets
    Mary.pets ={'cats' : [satan, tiger] , 'dogs': [rover]} #dictionary of lists of pets
    print("No. of pets mary has ")
    for x,y in Mary.pets.items():
        print(x, ":" , y)
    

    output

    Dog barks
    Cat meows
    No. of pets mary has 
    ('cats', ':', [Satan, tiger])
    ('dogs', ':', [Rover])