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;
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
add a comment |
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
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
add a comment |
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
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
python oop inheritance
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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
Okay, I see now. This makes a lot more sense. Thank you!
– Brent
Mar 8 at 4:20
add a comment |
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)
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
Okay, I see now. This makes a lot more sense. Thank you!
– Brent
Mar 8 at 4:20
add a comment |
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
Okay, I see now. This makes a lot more sense. Thank you!
– Brent
Mar 8 at 4:20
add a comment |
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
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
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
add a comment |
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
add a comment |
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)
add a comment |
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)
add a comment |
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)
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)
edited Mar 8 at 4:41
answered Mar 8 at 4:06
ap288ap288
365
365
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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