DonnellyOverflow
DonnellyOverflow

Reputation: 4195

__str__ method in Python

I can't manage to get this __str__ to work. I created a class,

class Hangman : 

and then

def __str__(self) : 
return "WORD: " + self.theWord + "; you have " + \
self.numberOfLives + "lives left"

there's an init statement and assignment in the program but I can't get the thing to work! the only way I can do it is by doing this, but surely what's the point of using __str__

def __str__(self) :
    print("WORD: {0}; you have {1} lives left".\
    format(self.theWord,self.numberOfLives))

Code:

theWord = input('Enter a word ')
numberOfLives = input('Enter a number ')
hangman = Hangman(theWord,numberOfLives)
Hangman.__str__(hangman)

Output:

>>> 
Enter a word Word
Enter a number 16
>>> 

using the print method, output:

>>> 
Enter a word word
Enter a number 16
WORD: word; you have 16 lives left
>>> 

Upvotes: 3

Views: 7287

Answers (4)

BioGeek
BioGeek

Reputation: 22827

This code works:

class Hangman(object):

    def __init__(self, theWord, numberOfLives):
        self.theWord = theWord
        self.numberOfLives = numberOfLives

    def __str__(self) :
        return "WORD: " + self.theWord + "; you have " + \
               self.numberOfLives + " lives left"

if __name__ == '__main__':
    theWord = input('Enter a word ')
    numberOfLives = input('Enter a number ')
    hangman = Hangman(theWord,numberOfLives)
    print(hangman)

Output:

>>> 
Enter a word word
Enter a number 16
WORD: word; you have 16 lives left

Upvotes: 0

Alberto Megía
Alberto Megía

Reputation: 2255

Like this post says: https://mail.python.org/pipermail/tutor/2004-September/031726.html

 >>> class A:
...   pass
...
 >>> a=A()
 >>> print a
<__main__.A instance at 0x007CF9E0>

If the class defines a __str__ method, Python will call it when you call 
str() or print:
 >>> class B:
...   def __str__(self):
...     return "I'm a B!"
...
 >>> b=B()
 >>> print b
I'm a B!

Quoting

To recap: when you tell Python to "print b", Python calls str(b) to get the string representation of b. If the class of b has a __str__ method, str(b) becomes a call to b.__str__(). This returns the string to print.

Upvotes: 0

Ben
Ben

Reputation: 71450

Hangman.__str__(hangman) isn't a command to print the string representation of hangman, it's an expression that evaluates to the string representation of hangman.

If you type that manually at the interactive prompt, you'll get the value of the expression printed, because the interactive prompt does that as a convenience. Having that line in a script (or in a function you call) will not print anything - you need to actually tell python to print it, with print(hangman).

Upvotes: 0

poke
poke

Reputation: 387647

Hangman.__str__(hangman)

This line will just call the __str__ method. So does this btw. which is the preferred way to do it (in general, don’t call special methods directly):

str(hangman)

str and the __str__ method are just there to convert the object into a string, but not to print it. For example you could just as well log it to a file, so printing wouldn’t always be appropriate.

Instead, if you want to print it, just print it:

print(hangman)

print will automatically call str() on the object and as such use the type’s __str__ method to convert it to a string.

Upvotes: 5

Related Questions