提交 1ea66aa7 编写于 作者: W wizardforcel

2.8.

上级 67f4ab5c
# Object-oriented programming
# 面向对象编程
# The big reveal
So far we've been working with functions and packages of functions, as well as defining our own functions. It turns out, though, that we've been working with objects all along, we just haven't recognize them as such. For example,
## 大揭秘
到目前为止,我们一直在使用函数和函数包,以及定义我们自己的函数。 但事实证明,我们一直在使用对象,我们只是没有认识到它们。 例如,
```python
x = 'Hi'
......@@ -13,14 +12,7 @@ x.lower()
# 'hi'
```
The string `x` is an object that we can send messages to.
字符串`x`是我们可以发送消息的对象。
```python
print( type(x) )
......@@ -28,9 +20,7 @@ print( type(x) )
# <class 'str'>
```
Even integers are objects:
甚至整数都是对象:
```python
print(dir(99))
......@@ -38,12 +28,9 @@ print(dir(99))
# ['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', '__index__', '__init__', '__init_subclass__', '__int__', '__invert__', '__le__', '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'bit_length', 'conjugate', 'denominator', 'from_bytes', 'imag', 'numerator', 'real', 'to_bytes']
```
*类*是对象的蓝图,基本上是类型的名称,在这种情况下是`str`。 对象称为类的*实例*
A *class* is the blueprint for an object and is basically the name of the type, `str` in this case. An object is called an *instance* of the class.
In `x.lower()` we are sending the `lower` message to the `x` string object. Messages are really just functions associated with classes/objects.
`x.lower()`中,我们将`lower`消息发送到`x`字符串对象。 消息实际上只是与类/对象相关的函数。
```python
x.lower
......@@ -51,20 +38,13 @@ x.lower
# <function str.lower>
```
In a language that does not support object learning programming, we would do something like:
在不支持对象学习编程的语言中,我们会做类似的事情:
```python
lower(x)
```
Python has both functions and object reprogramming which is why there is both `x.lower()` and:
Python 有函数和对象重编程,这就是为什么有`x.lower()`和:
```python
len(x)
......@@ -73,19 +53,13 @@ len(x)
```
函数或“消息”的选择取决于库设计者,但是`lower`仅对字符串有意义,因此将它与`str`的定义组合起来是有意义的。
然而,在实现方面,`x.lower()`实际上实现为`str.lower(x)`其中`str`是字符串的类定义。 电脑处理器了解函数调用; 他们不理解对象,所以我们在 Python 解释器本身中执行了这个翻译。
# 包 VS 对象成员
The choice of function or "message" is up to the library designer, but `lower` only makes sense for strings so it makes sense to group it with the definition of `str`.
In terms of implementation, however, `x.lower()` is actually implemented as `str.lower(x)` where `str` is the class definition for strings. Computer processors understand function calls; they do not understand objects and so we performed this translation within the Python interpreter itself.
# Package vs object members
Let's get another thing straight. The dot `.` operator is overloaded in Python to mean both package member and object member access. You are familiar with this already:
让我们直截了当。点`.`运算符在 Python 中重载,表示包成员和对象成员访问。你已经熟悉了这个:
```python
import numpy as np
......@@ -95,12 +69,6 @@ np.array([1,2,3])
```
```python
import math
math.log(3000)
......@@ -108,19 +76,13 @@ math.log(3000)
# 8.006367567650246
```
阅读代码时,这是一个常见的混淆点。 当我们看到`a.f()`时,我们不知道函数`f`是由`a`标识的包的成员,还是由`a`引用的对象的成员。
`wordsim`项目中,你定义了一个名为`wordsim.py`的文件,然后我的`test_wordsim.py`文件执行`from wordsim import *`来导入`wordsim.py`中的所有函数。
## 练习
This is a common point of confusion when reading code. When we see `a.f()`, we don't know whether that function `f` is a member of the package identified by `a` or an object referred to by `a`.
In the wordsim project, you defined a file called `wordsim.py` and then my `test_wordsim.py` file did `from wordsim import *` to pull in all your functions in `wordsim.py`.
## Exercise
In the following, identify the *identifiers* (words) as package or function or field:
在下文中,将*标识符*(单词)标识为包或函数或字段:
1. `np.log(3)`
1. `np.linalg.norm(v)`
......@@ -133,7 +95,7 @@ In the following, identify the *identifiers* (words) as package or function or f
1. `np.pi`
1. `img = img.convert("L")`
Now, identify the data types of subexpressions and identify the *identifiers* (words) as package or function or field:
现在,确定子表达式的数据类型,并将*标识符*(单词)标识为包或函数或字段:
1. `df["saledate"].dt.year`
1. `df_train.isnull().any().head(60)`
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册