How can I implement a method to print an object in the super class?How to flush output of print function?How can I safely create a nested directory in Python?How can I remove a trailing newline in Python?How to print without newline or space?How can I make a time delay in Python?Understanding Python super() with __init__() methodsHow to know if an object has an attribute in PythonNaming Classes - How to avoid calling everything a “<WhatEver>Manager”?Python class inherits objectHow to print to stderr in Python?

How is the claim "I am in New York only if I am in America" the same as "If I am in New York, then I am in America?

Can divisibility rules for digits be generalized to sum of digits

How old can references or sources in a thesis be?

Writing rule stating superpower from different root cause is bad writing

Mage Armor with Defense fighting style (for Adventurers League bladeslinger)

What's the point of deactivating Num Lock on login screens?

Why was the small council so happy for Tyrion to become the Master of Coin?

How is it possible to have an ability score that is less than 3?

What is the word for reserving something for yourself before others do?

How to format long polynomial?

TGV timetables / schedules?

Approximately how much travel time was saved by the opening of the Suez Canal in 1869?

What is the offset in a seaplane's hull?

Do VLANs within a subnet need to have their own subnet for router on a stick?

How did the USSR manage to innovate in an environment characterized by government censorship and high bureaucracy?

How do we improve the relationship with a client software team that performs poorly and is becoming less collaborative?

Why don't electron-positron collisions release infinite energy?

To string or not to string

"to be prejudice towards/against someone" vs "to be prejudiced against/towards someone"

Finding angle with pure Geometry.

tikz: show 0 at the axis origin

Fencing style for blades that can attack from a distance

Can I ask the recruiters in my resume to put the reason why I am rejected?

Arthur Somervell: 1000 Exercises - Meaning of this notation



How can I implement a method to print an object in the super class?


How to flush output of print function?How can I safely create a nested directory in Python?How can I remove a trailing newline in Python?How to print without newline or space?How can I make a time delay in Python?Understanding Python super() with __init__() methodsHow to know if an object has an attribute in PythonNaming Classes - How to avoid calling everything a “<WhatEver>Manager”?Python class inherits objectHow to print to stderr in Python?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















When I run this, I get the desired string corresponding to the species object that is created. My goal here is to be able to create a method called speak in the Animal class that achieves the same thing. But I don't have a good understanding on how to do that.



class Animal(object):

def __init__(self):
pass

def speak():
pass

class Mammal(Animal):

def __init__(self):
Animal.__init__(self)

class Cat(Mammal):

def __init__(self):
Mammal.__init__(self)

def __str__(self):
return "meeeow"

class Dog(Mammal):

def __init__(self):
Mammal.__init__(self)

def __str__(self):
return "wooof"

class Primate(Mammal):

def __init__(self):
Mammal.__init__(self)

class Hacker(Primate):

def __init__(self):
Primate.__init__(self)

def __str__(self):
return "Hello world!"

garfield = Cat()
print(garfield)

spike = Dog()
print(spike)

john = Hacker()
print(john)









share|improve this question






















  • What is the problem with creating a method with another name but same code?

    – Michael Butscher
    Mar 8 at 4:05












  • The name of the method could be anything, I just need to be able to call cat_name.speak() and and it return the string associated with that subclass.

    – Brent
    Mar 8 at 4:15

















0















When I run this, I get the desired string corresponding to the species object that is created. My goal here is to be able to create a method called speak in the Animal class that achieves the same thing. But I don't have a good understanding on how to do that.



class Animal(object):

def __init__(self):
pass

def speak():
pass

class Mammal(Animal):

def __init__(self):
Animal.__init__(self)

class Cat(Mammal):

def __init__(self):
Mammal.__init__(self)

def __str__(self):
return "meeeow"

class Dog(Mammal):

def __init__(self):
Mammal.__init__(self)

def __str__(self):
return "wooof"

class Primate(Mammal):

def __init__(self):
Mammal.__init__(self)

class Hacker(Primate):

def __init__(self):
Primate.__init__(self)

def __str__(self):
return "Hello world!"

garfield = Cat()
print(garfield)

spike = Dog()
print(spike)

john = Hacker()
print(john)









share|improve this question






















  • What is the problem with creating a method with another name but same code?

    – Michael Butscher
    Mar 8 at 4:05












  • The name of the method could be anything, I just need to be able to call cat_name.speak() and and it return the string associated with that subclass.

    – Brent
    Mar 8 at 4:15













0












0








0


1






When I run this, I get the desired string corresponding to the species object that is created. My goal here is to be able to create a method called speak in the Animal class that achieves the same thing. But I don't have a good understanding on how to do that.



class Animal(object):

def __init__(self):
pass

def speak():
pass

class Mammal(Animal):

def __init__(self):
Animal.__init__(self)

class Cat(Mammal):

def __init__(self):
Mammal.__init__(self)

def __str__(self):
return "meeeow"

class Dog(Mammal):

def __init__(self):
Mammal.__init__(self)

def __str__(self):
return "wooof"

class Primate(Mammal):

def __init__(self):
Mammal.__init__(self)

class Hacker(Primate):

def __init__(self):
Primate.__init__(self)

def __str__(self):
return "Hello world!"

garfield = Cat()
print(garfield)

spike = Dog()
print(spike)

john = Hacker()
print(john)









share|improve this question














When I run this, I get the desired string corresponding to the species object that is created. My goal here is to be able to create a method called speak in the Animal class that achieves the same thing. But I don't have a good understanding on how to do that.



class Animal(object):

def __init__(self):
pass

def speak():
pass

class Mammal(Animal):

def __init__(self):
Animal.__init__(self)

class Cat(Mammal):

def __init__(self):
Mammal.__init__(self)

def __str__(self):
return "meeeow"

class Dog(Mammal):

def __init__(self):
Mammal.__init__(self)

def __str__(self):
return "wooof"

class Primate(Mammal):

def __init__(self):
Mammal.__init__(self)

class Hacker(Primate):

def __init__(self):
Primate.__init__(self)

def __str__(self):
return "Hello world!"

garfield = Cat()
print(garfield)

spike = Dog()
print(spike)

john = Hacker()
print(john)






python oop inheritance






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 8 at 3:58









BrentBrent

136




136












  • What is the problem with creating a method with another name but same code?

    – Michael Butscher
    Mar 8 at 4:05












  • The name of the method could be anything, I just need to be able to call cat_name.speak() and and it return the string associated with that subclass.

    – Brent
    Mar 8 at 4:15

















  • What is the problem with creating a method with another name but same code?

    – Michael Butscher
    Mar 8 at 4:05












  • The name of the method could be anything, I just need to be able to call cat_name.speak() and and it return the string associated with that subclass.

    – Brent
    Mar 8 at 4:15
















What is the problem with creating a method with another name but same code?

– Michael Butscher
Mar 8 at 4:05






What is the problem with creating a method with another name but same code?

– Michael Butscher
Mar 8 at 4:05














The name of the method could be anything, I just need to be able to call cat_name.speak() and and it return the string associated with that subclass.

– Brent
Mar 8 at 4:15





The name of the method could be anything, I just need to be able to call cat_name.speak() and and it return the string associated with that subclass.

– Brent
Mar 8 at 4:15












2 Answers
2






active

oldest

votes


















1














Yours inherited classes can access any method defined in the parent class. So to achieve this, just define a new method called speak in Animal class and print as you did in your examples:



class Animal(object):

def __init__(self):
print(self)
pass

def speak(self):
print(self)


Then just create the instance and call the function:



garfield = Cat()
garfield.speak()


Output



meeeow





share|improve this answer























  • Okay, I see now. This makes a lot more sense. Thank you!

    – Brent
    Mar 8 at 4:20


















1














Modify your Animal class like below. When you call the super method from your subclass, you're passing the instance of the class you created to the parent's init function. So the instance of class is passed to the Mammal init function, which calls super and passes the instance to the Animal init function.



class Animal(object):

def __init__(self):
self.speak()

def speak(self):
print(self)





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55056507%2fhow-can-i-implement-a-method-to-print-an-object-in-the-super-class%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Yours inherited classes can access any method defined in the parent class. So to achieve this, just define a new method called speak in Animal class and print as you did in your examples:



    class Animal(object):

    def __init__(self):
    print(self)
    pass

    def speak(self):
    print(self)


    Then just create the instance and call the function:



    garfield = Cat()
    garfield.speak()


    Output



    meeeow





    share|improve this answer























    • Okay, I see now. This makes a lot more sense. Thank you!

      – Brent
      Mar 8 at 4:20















    1














    Yours inherited classes can access any method defined in the parent class. So to achieve this, just define a new method called speak in Animal class and print as you did in your examples:



    class Animal(object):

    def __init__(self):
    print(self)
    pass

    def speak(self):
    print(self)


    Then just create the instance and call the function:



    garfield = Cat()
    garfield.speak()


    Output



    meeeow





    share|improve this answer























    • Okay, I see now. This makes a lot more sense. Thank you!

      – Brent
      Mar 8 at 4:20













    1












    1








    1







    Yours inherited classes can access any method defined in the parent class. So to achieve this, just define a new method called speak in Animal class and print as you did in your examples:



    class Animal(object):

    def __init__(self):
    print(self)
    pass

    def speak(self):
    print(self)


    Then just create the instance and call the function:



    garfield = Cat()
    garfield.speak()


    Output



    meeeow





    share|improve this answer













    Yours inherited classes can access any method defined in the parent class. So to achieve this, just define a new method called speak in Animal class and print as you did in your examples:



    class Animal(object):

    def __init__(self):
    print(self)
    pass

    def speak(self):
    print(self)


    Then just create the instance and call the function:



    garfield = Cat()
    garfield.speak()


    Output



    meeeow






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Mar 8 at 4:09









    Hemerson TaconHemerson Tacon

    1,4521519




    1,4521519












    • Okay, I see now. This makes a lot more sense. Thank you!

      – Brent
      Mar 8 at 4:20

















    • Okay, I see now. This makes a lot more sense. Thank you!

      – Brent
      Mar 8 at 4:20
















    Okay, I see now. This makes a lot more sense. Thank you!

    – Brent
    Mar 8 at 4:20





    Okay, I see now. This makes a lot more sense. Thank you!

    – Brent
    Mar 8 at 4:20













    1














    Modify your Animal class like below. When you call the super method from your subclass, you're passing the instance of the class you created to the parent's init function. So the instance of class is passed to the Mammal init function, which calls super and passes the instance to the Animal init function.



    class Animal(object):

    def __init__(self):
    self.speak()

    def speak(self):
    print(self)





    share|improve this answer





























      1














      Modify your Animal class like below. When you call the super method from your subclass, you're passing the instance of the class you created to the parent's init function. So the instance of class is passed to the Mammal init function, which calls super and passes the instance to the Animal init function.



      class Animal(object):

      def __init__(self):
      self.speak()

      def speak(self):
      print(self)





      share|improve this answer



























        1












        1








        1







        Modify your Animal class like below. When you call the super method from your subclass, you're passing the instance of the class you created to the parent's init function. So the instance of class is passed to the Mammal init function, which calls super and passes the instance to the Animal init function.



        class Animal(object):

        def __init__(self):
        self.speak()

        def speak(self):
        print(self)





        share|improve this answer















        Modify your Animal class like below. When you call the super method from your subclass, you're passing the instance of the class you created to the parent's init function. So the instance of class is passed to the Mammal init function, which calls super and passes the instance to the Animal init function.



        class Animal(object):

        def __init__(self):
        self.speak()

        def speak(self):
        print(self)






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 8 at 4:41

























        answered Mar 8 at 4:06









        ap288ap288

        365




        365



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55056507%2fhow-can-i-implement-a-method-to-print-an-object-in-the-super-class%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            AWS Lex not identifying response if by a variable The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experienceEnforcing custom enumeration in AWS LEX for slot valuesHow to give response based on user response in Amazon Lex?Intercepting AWS Lambda Response to a AWS Lex QueryLex chat bot error: Reached second execution of fulfillment lambda on the same utteranceamazon lex showing invalid responseLambda response send back to Lex slot?Response card in Amazon lexAmazon Lex - Lambda response return HTML to botHow can I solve 424 (Failed Dependency) (python) obtained from Amazon lex?

            Алба-Юлія

            Захаров Федір Захарович