The difference between str() and _str_, repr() and _repr_:
str() and repr() are built-in functions in python and are functions directly used to format strings.
_ str_ and repr_ are strings of the class (object) itself in the class (object).
str
1 >>>help(str) 2 Help on class str in module builtins: 3 4 class str(object) 5 | str(object='') -> str 6 | str(bytes_or_buffer[, encoding[, errors]]) -> str 7 | 8 | Create a new string object from the given object. If encoding or 9 | errors is specified, then the object must expose a data buffer 10 | that will be decoded using the given encoding and error handler. 11 | Otherwise, returns the result of object.__str__() (if defined) 12 | or repr(object). 13 | encoding defaults to sys.getdefaultencoding(). 14 | errors defaults to 'strict'.
Create a new string object from a given object.
If an encoding or error is specified, the object must expose a data buffer that will be decoded using the given encoding and error handler.
Otherwise, the result of the object is returned.
_ str_() (if defined) or represents (object). The default getdefaultencoding() encoding system. The default error is "strict".
repr
1 >>>help(repr) 2 3 Help on built-in function repr in module builtins: 4 5 repr(obj, /) 6 Return the canonical string representation of the object. 7 8 For many object types, including most builtins, eval(repr(obj)) == obj.
First, try to generate such a string and pass it to eval() to regenerate the same object.
Otherwise, a string enclosed in angle brackets is generated, containing the type name and additional information (such as address)
eval
1 >>>help(eval) 2 3 Help on built-in function eval in module builtins: 4 5 eval(source, globals=None, locals=None, /) 6 Evaluate the given source in the context of globals and locals. 7 8 The source may be a string representing a Python expression 9 or a code object as returned by compile(). 10 The globals must be a dictionary and locals can be any mapping, 11 defaulting to the current globals and locals. 12 If only globals is given, locals defaults to it.
The code is as follows:
1 >>> eval('1+2') 2 3 3 >>> str('1+2') 4 '1+2' 5 >>> repr('1+2') 6 "'1+2'" 7 >>> type(repr('1+2')) 8 <class 'str'> 9 >>> type(str('1+2')) 10 <class 'str'> 11 >>> type(eval('1+2')) 12 <class 'int'> 13 #That's equivalent to what I said above. eval(repr(object)) == object 14 >>> eval(repr('1+2')) 15 '1+2' 16 >>> '1+2' 17 '1+2'
Example:
Python defines two methods _str_() and _repr_(), _str_() for display to users and _repr_() for display to developers.
1 class Person(object): 2 def __init__(self, name, sex): 3 self.name = name 4 self.sex = sex 5 def __str__(self): 6 return '(Person: %s, %s)' % (self.name, self.sex) 7 8 class Student(Person): 9 def __init__(self, name, sex, score): 10 Person.__init__(self, name, sex) 11 self.score = score 12 def __str__(self): 13 return '(Student: %s, %s, %s)' % (self.name, self.sex, self.score) 14 def __repr__(self): 15 return '(Student: %s, %s, %s)' % (self.name, self.sex, self.score)
1 >>> from demo import Person, Student 2 >>> p = Person('Alice', 'Female') 3 >>> p 4 <demo.Person object at 0x103eac0b8> 5 >>> print (p) 6 (Person: Alice, Female) 7 >>> s = Student('Tom', 'male', 20) 8 >>> s 9 (Student: Tom, male, 20) 10 >>> print (s) 11 (Student: Tom, male, 20)