Search code examples
pythonobjectmethodscall

When I call two methods, one from the parent class and one from the child class, only the first call returns


class Smarthphone():
    def __init__(self, tamanho, interface):
        self.tamanho = tamanho
        self.interface = interface
        
    def print_test(self):
        return "testing parent class"

class MP3Player(Smarthphone):
    def __init__(self, tamanho, interface, capacidade):
        super().__init__(tamanho, interface)
        self.capacidade = capacidade
        
    def print_MP3Player(self):
        return f"tamanho:{self.tamanho} interface:{self.interface} capacidade:{self.capacidade}"
        
        

ob1 = MP3Player(5, 'led', '240GB')

ob1.print_test()
ob1.print_MP3Player()

output:

'tamanho:5 interface:led capacidade:240GB'

If I swap the calls order the output would be 'testing parent class'.

Why does this happen?


Solution

  • You aren't printing anything. Whatever UI you are using is just showing you the last value the script produced. Do it the right way:

    print(ob1.print_test())
    print(ob1.print_MP3Player())