Why is .format giving me this weird output in my inherited class object? (New to python)
I am trying to write a toString() method in an inherited class but it is giving me weird output and I don't know why. New to Python, please help.
This is part of the tutorial Python Programming: https://www.youtube.com/watch?v=N4mEzFDjqtA&feature=youtu.be at minute 39. I tried changing the syntax a bit but the problem remained. Also, it didn't work with a direct call to the variables, as it is done in the video, but I had to use the get methods instead. Not sure why it works for him but not for me.
class Animal:
__name = ""
__height = 0
__weight = 0
__sound = 0
def __init__(self, name, height, weight, sound):
self.__name = name
self.__height = height
self.__weight = weight
self.__sound = sound
def get_name(self):
return self.__name
def get_height(self):
return self.__height
def get_weight(self):
return self.__weight
def get_sound(self):
return self.__sound
def toString(self):
return "{} is {} cm tall and {} kilograms and says {}".format(self.__name, self.__height, self.__weight, self.__sound)
class Dog(Animal):
__owner = ""
def __init__(self, name, height, weight, sound, owner):
self.__owner = owner
Animal.__init__(self, name,height,weight,sound)
def toString(self):
return "{} is {} cm tall and {} kilograms and says {}. His owner is {}".format(self.get_name, self.get_height, self.get_weight, self.get_sound, self.__owner)
cat = Animal('Whiskers', 33, 10, 'Meow')
print(cat.toString())
dog = Dog('Dida', 33, 10, 'Waf', 'Joshua')
print(dog.toString())
This gives me a result with things like: <main.Dog object at 0x00000243E9870390>> instead of the variables for the dog.toString() output. Instead of the actual variables.
Should be:
Whiskers is 33 cm tall and 10 kilograms and says Meow
Dida is 33 cm tall and 10 kilograms and says Meow. His owner is Joshua
Anyone can explain to me what I am doing wrong?
python
New contributor
|
show 5 more comments
I am trying to write a toString() method in an inherited class but it is giving me weird output and I don't know why. New to Python, please help.
This is part of the tutorial Python Programming: https://www.youtube.com/watch?v=N4mEzFDjqtA&feature=youtu.be at minute 39. I tried changing the syntax a bit but the problem remained. Also, it didn't work with a direct call to the variables, as it is done in the video, but I had to use the get methods instead. Not sure why it works for him but not for me.
class Animal:
__name = ""
__height = 0
__weight = 0
__sound = 0
def __init__(self, name, height, weight, sound):
self.__name = name
self.__height = height
self.__weight = weight
self.__sound = sound
def get_name(self):
return self.__name
def get_height(self):
return self.__height
def get_weight(self):
return self.__weight
def get_sound(self):
return self.__sound
def toString(self):
return "{} is {} cm tall and {} kilograms and says {}".format(self.__name, self.__height, self.__weight, self.__sound)
class Dog(Animal):
__owner = ""
def __init__(self, name, height, weight, sound, owner):
self.__owner = owner
Animal.__init__(self, name,height,weight,sound)
def toString(self):
return "{} is {} cm tall and {} kilograms and says {}. His owner is {}".format(self.get_name, self.get_height, self.get_weight, self.get_sound, self.__owner)
cat = Animal('Whiskers', 33, 10, 'Meow')
print(cat.toString())
dog = Dog('Dida', 33, 10, 'Waf', 'Joshua')
print(dog.toString())
This gives me a result with things like: <main.Dog object at 0x00000243E9870390>> instead of the variables for the dog.toString() output. Instead of the actual variables.
Should be:
Whiskers is 33 cm tall and 10 kilograms and says Meow
Dida is 33 cm tall and 10 kilograms and says Meow. His owner is Joshua
Anyone can explain to me what I am doing wrong?
python
New contributor
2
Please make the effort to reproduce your indentation accurately if you're going to post Python code. If people have to guess your indentation, they may miss problems or introduce new ones.
– khelwood
Dec 27 '18 at 15:39
2
if that's from an example in the tutorial, do yourself a favour and ditch the tutorial, it ain't worth a dime. This code is as totally unpythonic as it can be, what you're learning here is not Python but badly designed Java written in Python.
– bruno desthuilliers
Dec 27 '18 at 15:39
I second what Bruno is saying. Indeed, I left a comment on that video detailing exactly why it is a terrible Python tutorial
– juanpa.arrivillaga
Dec 27 '18 at 15:49
@khelwood Thank you for the feedback. But, what lines of code did I indent wrong? I can't see anything wrong with it.
– Samson Daniel
Dec 27 '18 at 15:56
1
@SamsonDaniel I confirm that this tutorial (at least the part on classes) is really really bad.
– bruno desthuilliers
Dec 27 '18 at 16:07
|
show 5 more comments
I am trying to write a toString() method in an inherited class but it is giving me weird output and I don't know why. New to Python, please help.
This is part of the tutorial Python Programming: https://www.youtube.com/watch?v=N4mEzFDjqtA&feature=youtu.be at minute 39. I tried changing the syntax a bit but the problem remained. Also, it didn't work with a direct call to the variables, as it is done in the video, but I had to use the get methods instead. Not sure why it works for him but not for me.
class Animal:
__name = ""
__height = 0
__weight = 0
__sound = 0
def __init__(self, name, height, weight, sound):
self.__name = name
self.__height = height
self.__weight = weight
self.__sound = sound
def get_name(self):
return self.__name
def get_height(self):
return self.__height
def get_weight(self):
return self.__weight
def get_sound(self):
return self.__sound
def toString(self):
return "{} is {} cm tall and {} kilograms and says {}".format(self.__name, self.__height, self.__weight, self.__sound)
class Dog(Animal):
__owner = ""
def __init__(self, name, height, weight, sound, owner):
self.__owner = owner
Animal.__init__(self, name,height,weight,sound)
def toString(self):
return "{} is {} cm tall and {} kilograms and says {}. His owner is {}".format(self.get_name, self.get_height, self.get_weight, self.get_sound, self.__owner)
cat = Animal('Whiskers', 33, 10, 'Meow')
print(cat.toString())
dog = Dog('Dida', 33, 10, 'Waf', 'Joshua')
print(dog.toString())
This gives me a result with things like: <main.Dog object at 0x00000243E9870390>> instead of the variables for the dog.toString() output. Instead of the actual variables.
Should be:
Whiskers is 33 cm tall and 10 kilograms and says Meow
Dida is 33 cm tall and 10 kilograms and says Meow. His owner is Joshua
Anyone can explain to me what I am doing wrong?
python
New contributor
I am trying to write a toString() method in an inherited class but it is giving me weird output and I don't know why. New to Python, please help.
This is part of the tutorial Python Programming: https://www.youtube.com/watch?v=N4mEzFDjqtA&feature=youtu.be at minute 39. I tried changing the syntax a bit but the problem remained. Also, it didn't work with a direct call to the variables, as it is done in the video, but I had to use the get methods instead. Not sure why it works for him but not for me.
class Animal:
__name = ""
__height = 0
__weight = 0
__sound = 0
def __init__(self, name, height, weight, sound):
self.__name = name
self.__height = height
self.__weight = weight
self.__sound = sound
def get_name(self):
return self.__name
def get_height(self):
return self.__height
def get_weight(self):
return self.__weight
def get_sound(self):
return self.__sound
def toString(self):
return "{} is {} cm tall and {} kilograms and says {}".format(self.__name, self.__height, self.__weight, self.__sound)
class Dog(Animal):
__owner = ""
def __init__(self, name, height, weight, sound, owner):
self.__owner = owner
Animal.__init__(self, name,height,weight,sound)
def toString(self):
return "{} is {} cm tall and {} kilograms and says {}. His owner is {}".format(self.get_name, self.get_height, self.get_weight, self.get_sound, self.__owner)
cat = Animal('Whiskers', 33, 10, 'Meow')
print(cat.toString())
dog = Dog('Dida', 33, 10, 'Waf', 'Joshua')
print(dog.toString())
This gives me a result with things like: <main.Dog object at 0x00000243E9870390>> instead of the variables for the dog.toString() output. Instead of the actual variables.
Should be:
Whiskers is 33 cm tall and 10 kilograms and says Meow
Dida is 33 cm tall and 10 kilograms and says Meow. His owner is Joshua
Anyone can explain to me what I am doing wrong?
python
python
New contributor
New contributor
edited Dec 27 '18 at 15:45
Ha Bom
6111418
6111418
New contributor
asked Dec 27 '18 at 15:22
Samson Daniel
313
313
New contributor
New contributor
2
Please make the effort to reproduce your indentation accurately if you're going to post Python code. If people have to guess your indentation, they may miss problems or introduce new ones.
– khelwood
Dec 27 '18 at 15:39
2
if that's from an example in the tutorial, do yourself a favour and ditch the tutorial, it ain't worth a dime. This code is as totally unpythonic as it can be, what you're learning here is not Python but badly designed Java written in Python.
– bruno desthuilliers
Dec 27 '18 at 15:39
I second what Bruno is saying. Indeed, I left a comment on that video detailing exactly why it is a terrible Python tutorial
– juanpa.arrivillaga
Dec 27 '18 at 15:49
@khelwood Thank you for the feedback. But, what lines of code did I indent wrong? I can't see anything wrong with it.
– Samson Daniel
Dec 27 '18 at 15:56
1
@SamsonDaniel I confirm that this tutorial (at least the part on classes) is really really bad.
– bruno desthuilliers
Dec 27 '18 at 16:07
|
show 5 more comments
2
Please make the effort to reproduce your indentation accurately if you're going to post Python code. If people have to guess your indentation, they may miss problems or introduce new ones.
– khelwood
Dec 27 '18 at 15:39
2
if that's from an example in the tutorial, do yourself a favour and ditch the tutorial, it ain't worth a dime. This code is as totally unpythonic as it can be, what you're learning here is not Python but badly designed Java written in Python.
– bruno desthuilliers
Dec 27 '18 at 15:39
I second what Bruno is saying. Indeed, I left a comment on that video detailing exactly why it is a terrible Python tutorial
– juanpa.arrivillaga
Dec 27 '18 at 15:49
@khelwood Thank you for the feedback. But, what lines of code did I indent wrong? I can't see anything wrong with it.
– Samson Daniel
Dec 27 '18 at 15:56
1
@SamsonDaniel I confirm that this tutorial (at least the part on classes) is really really bad.
– bruno desthuilliers
Dec 27 '18 at 16:07
2
2
Please make the effort to reproduce your indentation accurately if you're going to post Python code. If people have to guess your indentation, they may miss problems or introduce new ones.
– khelwood
Dec 27 '18 at 15:39
Please make the effort to reproduce your indentation accurately if you're going to post Python code. If people have to guess your indentation, they may miss problems or introduce new ones.
– khelwood
Dec 27 '18 at 15:39
2
2
if that's from an example in the tutorial, do yourself a favour and ditch the tutorial, it ain't worth a dime. This code is as totally unpythonic as it can be, what you're learning here is not Python but badly designed Java written in Python.
– bruno desthuilliers
Dec 27 '18 at 15:39
if that's from an example in the tutorial, do yourself a favour and ditch the tutorial, it ain't worth a dime. This code is as totally unpythonic as it can be, what you're learning here is not Python but badly designed Java written in Python.
– bruno desthuilliers
Dec 27 '18 at 15:39
I second what Bruno is saying. Indeed, I left a comment on that video detailing exactly why it is a terrible Python tutorial
– juanpa.arrivillaga
Dec 27 '18 at 15:49
I second what Bruno is saying. Indeed, I left a comment on that video detailing exactly why it is a terrible Python tutorial
– juanpa.arrivillaga
Dec 27 '18 at 15:49
@khelwood Thank you for the feedback. But, what lines of code did I indent wrong? I can't see anything wrong with it.
– Samson Daniel
Dec 27 '18 at 15:56
@khelwood Thank you for the feedback. But, what lines of code did I indent wrong? I can't see anything wrong with it.
– Samson Daniel
Dec 27 '18 at 15:56
1
1
@SamsonDaniel I confirm that this tutorial (at least the part on classes) is really really bad.
– bruno desthuilliers
Dec 27 '18 at 16:07
@SamsonDaniel I confirm that this tutorial (at least the part on classes) is really really bad.
– bruno desthuilliers
Dec 27 '18 at 16:07
|
show 5 more comments
1 Answer
1
active
oldest
votes
You have a mix of direct property access and methods, which are function. You can access properties directly like: self.__weight
but you need to call the methods: self.get_name()
(with ()
) otherwise you just print the string representation of the function itself.
A string method more like this should work better:
def toString(self):
return "{} is {} cm tall and {} kilograms and says {}. His owner is {}".format(self.get_name(), self.get_height(), self.get_weight(), self.get_sound(), self.__owner)
1
Thanks @Mark Meyer
– Samson Daniel
Dec 27 '18 at 15:43
@The_Flin I am using the Spyder environment and Python 3.7.0. It appears I can't answer your comment directly so that's why I am answering it here.
– Samson Daniel
Dec 27 '18 at 15:45
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
});
}
});
Samson Daniel is a new contributor. Be nice, and check out our Code of Conduct.
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%2f53947273%2fwhy-is-string-format-giving-me-this-weird-output-in-my-inherited-class-object%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You have a mix of direct property access and methods, which are function. You can access properties directly like: self.__weight
but you need to call the methods: self.get_name()
(with ()
) otherwise you just print the string representation of the function itself.
A string method more like this should work better:
def toString(self):
return "{} is {} cm tall and {} kilograms and says {}. His owner is {}".format(self.get_name(), self.get_height(), self.get_weight(), self.get_sound(), self.__owner)
1
Thanks @Mark Meyer
– Samson Daniel
Dec 27 '18 at 15:43
@The_Flin I am using the Spyder environment and Python 3.7.0. It appears I can't answer your comment directly so that's why I am answering it here.
– Samson Daniel
Dec 27 '18 at 15:45
add a comment |
You have a mix of direct property access and methods, which are function. You can access properties directly like: self.__weight
but you need to call the methods: self.get_name()
(with ()
) otherwise you just print the string representation of the function itself.
A string method more like this should work better:
def toString(self):
return "{} is {} cm tall and {} kilograms and says {}. His owner is {}".format(self.get_name(), self.get_height(), self.get_weight(), self.get_sound(), self.__owner)
1
Thanks @Mark Meyer
– Samson Daniel
Dec 27 '18 at 15:43
@The_Flin I am using the Spyder environment and Python 3.7.0. It appears I can't answer your comment directly so that's why I am answering it here.
– Samson Daniel
Dec 27 '18 at 15:45
add a comment |
You have a mix of direct property access and methods, which are function. You can access properties directly like: self.__weight
but you need to call the methods: self.get_name()
(with ()
) otherwise you just print the string representation of the function itself.
A string method more like this should work better:
def toString(self):
return "{} is {} cm tall and {} kilograms and says {}. His owner is {}".format(self.get_name(), self.get_height(), self.get_weight(), self.get_sound(), self.__owner)
You have a mix of direct property access and methods, which are function. You can access properties directly like: self.__weight
but you need to call the methods: self.get_name()
(with ()
) otherwise you just print the string representation of the function itself.
A string method more like this should work better:
def toString(self):
return "{} is {} cm tall and {} kilograms and says {}. His owner is {}".format(self.get_name(), self.get_height(), self.get_weight(), self.get_sound(), self.__owner)
answered Dec 27 '18 at 15:30
Mark Meyer
35.8k32958
35.8k32958
1
Thanks @Mark Meyer
– Samson Daniel
Dec 27 '18 at 15:43
@The_Flin I am using the Spyder environment and Python 3.7.0. It appears I can't answer your comment directly so that's why I am answering it here.
– Samson Daniel
Dec 27 '18 at 15:45
add a comment |
1
Thanks @Mark Meyer
– Samson Daniel
Dec 27 '18 at 15:43
@The_Flin I am using the Spyder environment and Python 3.7.0. It appears I can't answer your comment directly so that's why I am answering it here.
– Samson Daniel
Dec 27 '18 at 15:45
1
1
Thanks @Mark Meyer
– Samson Daniel
Dec 27 '18 at 15:43
Thanks @Mark Meyer
– Samson Daniel
Dec 27 '18 at 15:43
@The_Flin I am using the Spyder environment and Python 3.7.0. It appears I can't answer your comment directly so that's why I am answering it here.
– Samson Daniel
Dec 27 '18 at 15:45
@The_Flin I am using the Spyder environment and Python 3.7.0. It appears I can't answer your comment directly so that's why I am answering it here.
– Samson Daniel
Dec 27 '18 at 15:45
add a comment |
Samson Daniel is a new contributor. Be nice, and check out our Code of Conduct.
Samson Daniel is a new contributor. Be nice, and check out our Code of Conduct.
Samson Daniel is a new contributor. Be nice, and check out our Code of Conduct.
Samson Daniel is a new contributor. Be nice, and check out our Code of Conduct.
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53947273%2fwhy-is-string-format-giving-me-this-weird-output-in-my-inherited-class-object%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
2
Please make the effort to reproduce your indentation accurately if you're going to post Python code. If people have to guess your indentation, they may miss problems or introduce new ones.
– khelwood
Dec 27 '18 at 15:39
2
if that's from an example in the tutorial, do yourself a favour and ditch the tutorial, it ain't worth a dime. This code is as totally unpythonic as it can be, what you're learning here is not Python but badly designed Java written in Python.
– bruno desthuilliers
Dec 27 '18 at 15:39
I second what Bruno is saying. Indeed, I left a comment on that video detailing exactly why it is a terrible Python tutorial
– juanpa.arrivillaga
Dec 27 '18 at 15:49
@khelwood Thank you for the feedback. But, what lines of code did I indent wrong? I can't see anything wrong with it.
– Samson Daniel
Dec 27 '18 at 15:56
1
@SamsonDaniel I confirm that this tutorial (at least the part on classes) is really really bad.
– bruno desthuilliers
Dec 27 '18 at 16:07