Skip to content

Commit 799281a

Browse files
committed
8.1小节完成~
1 parent 64e0a30 commit 799281a

File tree

4 files changed

+108
-9
lines changed

4 files changed

+108
-9
lines changed

cookbook/c08/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env python
2+
# -*- encoding: utf-8 -*-
3+
"""
4+
Topic: sample
5+
Desc :
6+
"""
7+

cookbook/c08/p01_str_represent.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env python
2+
# -*- encoding: utf-8 -*-
3+
"""
4+
Topic: 实例的字符串显示
5+
Desc :
6+
"""
7+
8+
9+
class Pair:
10+
def __init__(self, x, y):
11+
self.x = x
12+
self.y = y
13+
14+
def __repr__(self):
15+
return 'Pair({0.x!r}, {0.y!r})'.format(self)
16+
17+
def __str__(self):
18+
return '({0.x!s}, {0.y!s})'.format(self)
19+
20+
p = Pair(3, 4)
21+
print(p)
22+
23+

source/c08/p01_change_string_representation_of_instances.rst

Lines changed: 75 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,86 @@
55
----------
66
问题
77
----------
8-
todo...
8+
你想改变对象实例的打印或显示输出,让它们更具可读性。
9+
10+
|
911
1012
----------
1113
解决方案
1214
----------
13-
todo...
15+
要改变一个实例的字符串表示,可重新定义它的 ``__str__()`` 和 ``__repr__()`` 方法。例如:
16+
17+
.. code-block:: python
18+
19+
class Pair:
20+
def __init__(self, x, y):
21+
self.x = x
22+
self.y = y
23+
24+
def __repr__(self):
25+
return 'Pair({0.x!r}, {0.y!r})'.format(self)
26+
27+
def __str__(self):
28+
return '({0.x!s}, {0.y!s})'.format(self)
29+
30+
``__repr__()`` 方法返回一个实例的代码表示形式,通常用来重新构造这个实例。
31+
内置的 ``repr()`` 函数返回这个字符串,跟我们使用交互式解释器显示的值是一样的。
32+
``__str__()`` 方法将实例转换为一个字符串,使用 ``str()`` 或 ``print()`` 函数会输出这个字符串。比如:
33+
34+
.. code-block:: python
35+
36+
>>> p = Pair(3, 4)
37+
>>> p
38+
Pair(3, 4) # __repr__() output
39+
>>> print(p)
40+
(3, 4) # __str__() output
41+
>>>
42+
43+
我们在这里还演示了在格式化的时候怎样使用不同的字符串表现形式。
44+
特别来讲,``!r`` 格式化代码指明输出使用 ``__repr__()`` 来代替默认的 ``__str__()`` 。
45+
你可以用前面的类来试着测试下:
46+
47+
.. code-block:: python
48+
49+
>>> p = Pair(3, 4)
50+
>>> print('p is {0!r}'.format(p))
51+
p is Pair(3, 4)
52+
>>> print('p is {0}'.format(p))
53+
p is (3, 4)
54+
>>>
55+
56+
|
1457
1558
----------
1659
讨论
1760
----------
18-
todo...
61+
自定义 ``__repr__()`` 和 ``__str__()`` 通常是很好的习惯,因为它能简化调试和实例输出。
62+
例如,如果仅仅只是打印输出或日志输出某个实例,那么程序员会看到实例更加详细与有用的信息。
63+
64+
``__repr__()`` 生成的文本字符串标准做法是需要让 ``eval(repr(x)) == x`` 为真。
65+
如果实在不能这样子做,应该创建一个有用的文本表示,并使用 < 和 > 括起来。比如:
66+
67+
.. code-block:: python
68+
69+
>>> f = open('file.dat')
70+
>>> f
71+
<_io.TextIOWrapper name='file.dat' mode='r' encoding='UTF-8'>
72+
>>>
73+
74+
如果 ``__str__()`` 没有被定义,那么就会使用 ``__repr__()`` 来代替输出。
75+
76+
上面的 ``format()`` 方法的使用看上去很有趣,格式化代码 ``{0.x}`` 对应的是第1个参数的x属性。
77+
因此,在下面的函数中,0实际上指的就是 ``self`` 本身:
78+
79+
.. code-block:: python
80+
81+
def __repr__(self):
82+
return 'Pair({0.x!r}, {0.y!r})'.format(self)
83+
84+
作为这种实现的一个替代,你也可以使用 ``%`` 操作符,就像下面这样:
85+
86+
.. code-block:: python
87+
88+
def __repr__(self):
89+
return 'Pair(%r, %r)' % (self.x, self.y)
90+

source/chapters/p08_classes_and_objects.rst

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@
22
第八章:类与对象
33
=============================
44

5-
Python provides a variety of useful built-in data structures, such as lists, sets, and dictionaries.
6-
For the most part, the use of these structures is straightforward. However,
7-
common questions concerning searching, sorting, ordering, and filtering often arise.
8-
Thus, the goal of this chapter is to discuss common data structures and algorithms
9-
involving data. In addition, treatment is given to the various data structures contained
10-
in the collections module.
5+
本章主要关注点的是和类定义有关的常见编程模型。包括让对象支持常见的Python特性、特殊方法的使用、
6+
类封装技术、继承、内存管理以及有用的设计模式。
7+
118

129
Contents:
1310

0 commit comments

Comments
 (0)