Differences and relationships between str () and _ str _, repr () and _ repr _ in Python, __str _ repr

Source: Internet
Author: User

Differences and relationships between str () and _ str _, repr () and _ repr _ in Python, __str _ repr
The difference between str () and _ str _, repr () and _ repr:

Both str () and repr () are built-in functions in python and are directly used to format strings.

_ Str _ and _ repr _ are strings processing 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 buffer10  |  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'.

Creates a new String object from a given object.

If 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 (object ). The default value is getdefaultencoding. 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, generate a string enclosed by Angle brackets, including the type name and additional information (such as the 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 # is equivalent to the eval (repr (object) = object14 >>> eval (repr ('1 + 2') mentioned above ')) 15 '1 + 2' 16 >>> '1 + 2' 17' 1 + 2'

Instance:

  Python defines two methods: _ str _ () and _ repr _ (). __str _ () is used for display to users, _ repr _ () is used to 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 = score12     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)

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.