内容简介:Python中str()与__str__、repr()与__repr__区别与关系
str()与__str__、repr()与__repr__ 的区别:
str()与repr()都是 python 中的内置函数,是直接用来格式化字符串的函数。
__str__与__repr__ 是在类(对象)中对类(对象)本身进行字符串处理。
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'.
从给定对象创建一个新字符串对象。
如果指定了编码或错误,则对象必须公开一个将使用给定的编码和错误处理程序进行解码的数据缓冲区。
否则,返回对象的结果。
__str__()(如果定义)或代表(对象)。默认getdefaultencoding()编码系统。错误默认为“严格”。
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.
首先,尝试生成这样一个字符串,将其传给 eval()可重新生成同样的对象
否则,生成用尖括号包住的字符串,包含类型名和额外的信息(比如地址)
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.
代码如下:
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 #就相当于上面说的eval(repr(object)) == object
14 >>> eval(repr('1+2'))
15 '1+2'
16 >>> '1+2'
17 '1+2'
实例:
Python 定义了__str__()和__repr__()两种方法,__str__()用于显示给用户,而__repr__()用于显示给开发人员。
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)
以上所述就是小编给大家介绍的《Python中str()与__str__、repr()与__repr__区别与关系》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- 框架与RTTI的关系,RTTI与反射之间的关系
- 如何用循环关系网络机智地解决数独类关系推理任务?
- 【mybatis xml】数据层框架应用--Mybatis(三)关系映射之一对一关系映射
- Hibernate 关系映射整理
- Hibernate 关系映射整理
- 简单实用UML关系图解
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
数据挖掘概念与技术
(加)Jiawei Han;Micheline Kamber / 范明、孟小峰 / 机械工业 / 2007-3 / 55.00元
《数据挖掘概念与技术(原书第2版)》全面地讲述数据挖掘领域的重要知识和技术创新。在第1版内容相当全面的基础上,第2版展示了该领域的最新研究成果,例如挖掘流、时序和序列数据以及挖掘时间空间、多媒体、文本和Web数据。本书可作为数据挖掘和知识发现领域的教师、研究人员和开发人员的一本必读书。 《数据挖掘概念与技术(原书第2版)》第1版曾是受读者欢迎的数据挖掘专著,是一本可读性极佳的教材。第2版充实了数据......一起来看看 《数据挖掘概念与技术》 这本书的介绍吧!