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

How can I use the info from the parent class in a child object?

class Animal:
def eat(self):
print("eats well")
def sleep(self):
print("sleeps well")

class Dog(Animal):
def init(self,breed,color):
self.breed= breed
self.color= color
Canela=Dog("chihuahua","brown")

print("This dog is a",Canela.breed,"and its color is",Canela.color)

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

#untill there ir works but how can I add the information from the parent class, If I do
print("This dog is a",Canela.breed,"and its color is",Canela.color,"and",Canela.eat()) it appears It eats well at the top and none next to the sentence. How can I write it well?

>Solution :

that’s is because your eat function print to the stdout and does not return a string.
you need to change your eat function to be:

def eat(self):
    return "eats well"
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