3

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
>>> 
4
  • Define "can't get the thing to work" Commented Nov 27, 2013 at 20:49
  • sorry, it just just won't print like it's suppose to Commented Nov 27, 2013 at 20:50
  • Then give an example of actual output vs expected output. Commented Nov 27, 2013 at 20:51
  • 1
    Read this post -> mail.python.org/pipermail/tutor/2004-September/031726.html Commented Nov 27, 2013 at 20:53

4 Answers 4

5
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.

Sign up to request clarification or add additional context in comments.

1 Comment

Ahhhh! Can't believe it's that easy. Thanks a lot!
0

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).

Comments

0

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.