Why is .format giving me this weird output in my inherited class object? (New to python)












-1














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?










share|improve this question









New contributor




Samson Daniel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 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
















-1














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?










share|improve this question









New contributor




Samson Daniel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 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














-1












-1








-1







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?










share|improve this question









New contributor




Samson Daniel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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






share|improve this question









New contributor




Samson Daniel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Samson Daniel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited Dec 27 '18 at 15:45









Ha Bom

6111418




6111418






New contributor




Samson Daniel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Dec 27 '18 at 15:22









Samson Daniel

313




313




New contributor




Samson Daniel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Samson Daniel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Samson Daniel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 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




    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












1 Answer
1






active

oldest

votes


















4














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)





share|improve this answer

















  • 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











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.










draft saved

draft discarded


















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









4














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)





share|improve this answer

















  • 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
















4














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)





share|improve this answer

















  • 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














4












4








4






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)





share|improve this answer












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)






share|improve this answer












share|improve this answer



share|improve this answer










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














  • 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










Samson Daniel is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















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.




draft saved


draft discarded














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





















































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

Angular Downloading a file using contenturl with Basic Authentication

Olmecas

Can't read property showImagePicker of undefined in react native iOS