3

I have a custom class like:

class foo(object):
    def __init__(self, name):
        self.__name = name
    
    def get_name(self):
         return self.__name

What I want to do is to write

test = foo("test")
print test

instead of

test = foo("test")
print test.get_name()

What is the magic method for doing this?

andnik
  • 2,405
  • 2
  • 22
  • 33
alabamajack
  • 642
  • 8
  • 23
  • See [here](http://stackoverflow.com/questions/1436703/difference-between-str-and-repr-in-python). Also, your posted code seems faulty (``foo().get_name()``). – Ami Tavory May 19 '15 at 06:58
  • 1
    I'm not sure why this is attracting "unclear what you're asking" votes: the question is perfectly clear. Python has at least one method (actually two), which is called when a string representation of the class is needed, and the OP knows that one exists, but not its name. – GreenAsJade May 19 '15 at 08:47
  • @AmiTavory good pointer to `str` vs `repr`, but I'm not sure what is wrong with the OP's code. Parens are only needed for print statements in python3. – GreenAsJade May 19 '15 at 08:52
  • @GreenAsJade Thanks. I was referring to the need for parens after ``foo``. In the OP's code, ``foo`` is a class. – Ami Tavory May 19 '15 at 08:55
  • Hah! The actual typo is that it should be `print test.get_name()`. No parens, and no foo :) `foo()` in particular is not meaningful in this context (`foo` is a class, not a function). – GreenAsJade May 19 '15 at 08:58
  • Yup, you're right - that's the actual typo, probably. – Ami Tavory May 19 '15 at 09:13

3 Answers3

7

There are two methods that are relevant. There is the __str__ method which "converts" your object into a string. Then there is a __repr__ method which converts your object into a "programmer representation." The print function (or statement if you're using Python 2), uses the str function to convert the object into a string and then writes that to sys.stdout. str gets the string representation by first checking for the __str__ method and if it fails, it then checks the __repr__ method. You can implement them based on your convenience to get what you want.

In your own case, implementing a

def __str__(self):
   return self.__name

should be enough.

BSplitter
  • 117
  • 6
Noufal Ibrahim
  • 71,383
  • 13
  • 135
  • 169
  • thanks for this full explanation. If I understood you correctly, this means, that the `__str__` method will only be used if a function(like `print`) need the string representation of this class. So, in my example, it is no problem if I write `f = foo('test'); i = foo('i'); i = f;` than `i` is a `f` and not only the name of `i` changed?! – alabamajack May 19 '15 at 07:07
  • After your series of statements, `i` and `f` point to the same object (In this case `foo('test')`). The `foo('i')` object is now not referenced by anything. – Noufal Ibrahim May 19 '15 at 09:25
1

You need to implement __str__

class foo(object):
    def __init__(self, name):
        self.__name = name

    def get_name(self):
        return self.__name

    def __str__(self):
        return self.get_name()

f = foo("My name")

print f
Leon
  • 12,013
  • 5
  • 36
  • 59
1

You need to override __str__() method like this:

class b:
     def __str__(self)
             return "hello"

n = b()

print n
hello
Dmitry Zagorulkin
  • 8,370
  • 4
  • 37
  • 60