0

I have a function which is defined as below,

import pprint

class Test(object):
        def __str__(self):
                return "at __str__"

        def __repr__(self):
                return "at __repr__"

x = Test()

print x
pprint.pprint(x)

Output:

at __str__
at __repr__

Questions:

  1. what "def __str__(self):" representation means.
  2. Why the first print is printing the first return value where as pprint is returning second return value.

I'm pretty new to python programming, appreciate if you can provide more detailed information on this.

Siva Dasari
  • 1,059
  • 2
  • 19
  • 40
  • 2
    Isn't this code kind of self-explanatory? You have just demonstrated that `print` calls the object's `__str__` while `pprint.pprint` calls the object's `__repr__`. – TigerhawkT3 Apr 06 '17 at 23:41

1 Answers1

1

Difference between str and repr in Python is perfect answer of your question if you only want to know about __str__.but i suppose you may really want to know what is mean. It's called Operator Overloading.you can implement__abs__,then you can -x.readOperator Overloading question in stackoverflow

Community
  • 1
  • 1
obgnaw
  • 3,007
  • 11
  • 25