Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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())
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading