Files
ailearning/docs/da/105.md
2020-10-19 21:08:55 +08:00

319 lines
4.8 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 特殊方法
**Python** 使用 `__` 开头的名字来定义特殊的方法和属性,它们有:
* `__init__()`
* `__repr__()`
* `__str__()`
* `__call__()`
* `__iter__()`
* `__add__()`
* `__sub__()`
* `__mul__()`
* `__rmul__()`
* `__class__`
* `__name__`
## 构造方法 `__init__()`
之前说到,在产生对象之后,我们可以向对象中添加属性。事实上,还可以通过构造方法,在构造对象的时候直接添加属性:
In [1]:
```py
class Leaf(object):
"""
A leaf falling in the woods.
"""
def __init__(self, color='green'):
self.color = color
```
默认属性值:
In [2]:
```py
leaf1 = Leaf()
print leaf1.color
```
```py
green
```
传入有参数的值:
In [3]:
```py
leaf2 = Leaf('orange')
print leaf2.color
```
```py
orange
```
回到森林的例子:
In [4]:
```py
import numpy as np
class Forest(object):
""" Forest can grow trees which eventually die."""
def __init__(self):
self.trees = np.zeros((150,150), dtype=bool)
self.fires = np.zeros((150,150), dtype=bool)
```
我们在构造方法中定义了两个属性 `trees``fires`
In [5]:
```py
forest = Forest()
forest.trees
```
Out[5]:
```py
array([[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False],
...,
[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False]], dtype=bool)
```
In [6]:
```py
forest.fires
```
Out[6]:
```py
array([[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False],
...,
[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False]], dtype=bool)
```
修改属性的值:
In [7]:
```py
forest.trees[0,0]=True
forest.trees
```
Out[7]:
```py
array([[ True, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False],
...,
[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False]], dtype=bool)
```
改变它的属性值不会影响其他对象的属性值:
In [8]:
```py
forest2 = Forest()
forest2.trees
```
Out[8]:
```py
array([[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False],
...,
[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False]], dtype=bool)
```
事实上,`__new__()` 才是真正产生新对象的方法,`__init__()` 只是对对象进行了初始化,所以:
```py
leaf = Leaf()
```
相当于
```py
my_new_leaf = Leaf.__new__(Leaf)
Leaf.__init__(my_new_leaf)
leaf = my_new_leaf
```
## 表示方法 `__repr__()` 和 `__str__()`
In [9]:
```py
class Leaf(object):
"""
A leaf falling in the woods.
"""
def __init__(self, color='green'):
self.color = color
def __str__(self):
"This is the string that is printed."
return "A {} leaf".format(self.color)
def __repr__(self):
"This string recreates the object."
return "{}(color='{}')".format(self.__class__.__name__, self.color)
```
`__str__()` 是使用 `print` 函数显示的结果:
In [10]:
```py
leaf = Leaf()
print leaf
```
```py
A green leaf
```
`__repr__()` 返回的是不使用 `print` 方法的结果:
In [11]:
```py
leaf
```
Out[11]:
```py
Leaf(color='green')
```
回到森林的例子:
In [12]:
```py
import numpy as np
class Forest(object):
""" Forest can grow trees which eventually die."""
def __init__(self, size=(150,150)):
self.size = size
self.trees = np.zeros(self.size, dtype=bool)
self.fires = np.zeros((self.size), dtype=bool)
def __repr__(self):
my_repr = "{}(size={})".format(self.__class__.__name__, self.size)
return my_repr
def __str__(self):
return self.__class__.__name__
```
In [13]:
```py
forest = Forest()
```
`__str__()` 方法:
In [14]:
```py
print forest
```
```py
Forest
```
`__repr__()` 方法:
In [15]:
```py
forest
```
Out[15]:
```py
Forest(size=(150, 150))
```
`__name__``__class__` 为特殊的属性:
In [16]:
```py
forest.__class__
```
Out[16]:
```py
__main__.Forest
```
In [17]:
```py
forest.__class__.__name__
```
Out[17]:
```py
'Forest'
```