-1

I am making a simple class, here is the code:

class Player(object):
    def __init__(self, character):
        self.character= character       

Sara = Player("Sara")

Nothing fancy, when ever i run this it gives the following result:

>>> print Sara
<__main__.Player object at 0xxxxxxxxx>   

How can i stop the last line <main.Pl.....> from executing?

Ícaro
  • 1,432
  • 3
  • 18
  • 36
Belle
  • 451
  • 1
  • 4
  • 9

1 Answers1

1

You need to set the __repr__ and __str__ functions in your class so the print function knows what to print correctly. Please change your class to the example below and try again.

class Player(object):
    def __init__(self, character):
        self.character = character  

    def __repr__(self):
        return self.character

    def __str__(self):
        return self.character
Ícaro
  • 1,432
  • 3
  • 18
  • 36
  • It's working! The same way Sara.character would execute. Do you have any good articles on these two functions? – Belle May 29 '17 at 14:20
  • Sure, @Belle, you can take a look at Python's documentation for input and output: https://docs.python.org/2.7/tutorial/inputoutput.html. If my answer helped you please accept it as correct :-) – Ícaro May 29 '17 at 14:29