submit questions
When we customize a class, the class object print or type the information in this class object press Enter, the default is displayed directly in the interactive mode does not seem useful. As follows
In [1]: class People: ...: def __init__(self, name, sex): ...: self.name = name ...: self.sex = sex ...: In [2]: p = People('xiaoming', 'male') In [3]: p Out[3]: <__main__.People at 0x7fd0df745400> In [4]: print(p) <__main__.People object at 0x7fd0df745400>
Can be found in two ways displayed information is the object's class name and the address of the object is located, and very often this is not the information we want, then we can be rewritten by __str__ method and __repr__ customized information we want.
__str__ and __repr__ brief
__str__ and __repr__ methods, custom class of string descriptor , when we print or view an object, the results end up seeing is the return value of these two methods. These two methods are strings returned, but when calling __str__? When calling __repr__? We look at the following example
Only class overrides the method __str__
In [1]: class A: ...: def __str__(self): ...: return '__str__' ...: In [2]: a = A() In [3]: a Out[3]: <__main__.A at 0x7ffb3f2ba048> In [4]: print(a) __str__
In interactive mode, then enter directly into the object, the results returned by the same as the original; but when you print an object, it triggered __str__ method.
Only class overrides the method __repr__
In [1]: class B: ...: def __repr__(self): ...: return '__repr__' ...: In [2]: b = B() In [3]: b Out[3]: __repr__ In [4]: print(b) __repr__
In interactive mode, then enter directly into the object, and print objects are triggered __repr__ method.
__Str__ class overrides the method and __repr__
In [1]: class C: ...: def __str__(self): ...: return '__str__' ...: def __repr__(self): ...: return '__repr__' ...: In [2]: c = C() In [3]: c Out[3]: __repr__ In [4]: print(c) __str__
In interactive mode, then enter directly into the object, triggering a __repr__ method; print objects trigger __str__ method.
To summarize, when we direct view objects (input object and then Enter), no matter what the trigger is __repr__ method; if a custom class does not override this method, we all know Python3 in the new class, which is the default inherit the object class, then the time will call the method object of __repr__. When we print objects, most cases are triggered __str__ method, but only custom class overrides the method __repr__ triggered __repr__ method.
str () and the repr () function
str () Default triggers __str__ method, the repr () method __repr__ default trigger, the trigger mechanism as described above; in addition, lists, dictionaries and other containers will always trigger __repr__ method, the following sample code:
# Class method of rewriting only a __str__ the In [. 5 ]: STR (A) Out [ . 5]: ' __str__ ' the In [ . 6 ]: the repr (A) Out [ . 6]: ' <__ __ A main AT 0x7ffb3f2ba048 Object. > ' # class method of rewriting only a __repr__ the in [. 5 ]: STR (B) Out [ . 5]: ' __repr__ ' the in [ . 6 ]: the repr (B) Out [ . 6]:' __Repr__ ' # class overrides the method __repr__ __str__ and the In [. 5 ]: STR (C) Out [ . 5]: ' __str__ ' the In [ . 6 ]: the repr (C) Out [ . 6]: ' __repr__ ' # lists, dictionaries and other containers will always trigger __repr__ method of the In [53 is]: Print ([A]) [ < __main__ II.A Object AT 0x7ffb3f2ba048 > ] In [7]: str([c]) Out[7]: '[__repr__]' In [8]: repr([c]) Out[8]: '[__repr__]' In [9]: [c] Out[9]: [__repr__] In [10]: print([c]) [__repr__]
The essential difference __str__ and __repr__
That essential difference __str__ and __repr__ what in the end is it? Look at the official description
- __str__: Called by
str(object)
and the built-in functionsformat()
andprint()
to compute the “informal” or nicely printable string representation of an object. - __repr__: Called by the
repr()
built-in function to compute the “official” string representation of an object. This is typically used for debugging, so it is important that the representation is information-rich and unambiguous.
To summarize, __ str__ informal, easy to read a string describing the object, when the class is instantiated str (str (object)) will be called, will be built-in functions and format () and print () call; __repr__ string is the official description of an object, will be built-in function repr () method call, its description must be informative and clear. That result __str__ returned readable, __ results repr__ return more accurate. Look at an example
The In [. 1]: Import datetime the In [ 2]: = D datetime.datetime.now () the In [ . 3 ]: STR (D) Out [ . 3]: ' 2019-08-24 08: 12 is: 17.942242 ' # readable strong the In [ . 4 ]: the repr (D) Out [ . 4]: ' A datetime.datetime (2019,. 8, 24,. 8, 12 is,. 17, 942 242) ' # information is more accurate and abundant
to sum up
- In the custom class, class objects may be customized by overriding method to __repr__ __str__ and conversion of strings.
- In general, it is recommended to add at least one method __repr__ customized objects to define the conversion from a string, __ str__ is optional. Because the default when needed but can not find __str__ method will automatically call __repr__ method.
- Target __str__ approach is that readability __ target repr__ method is accuracy.