提交 b85b6382 编写于 作者: W wizardforcel

2.8.

上级 1ea66aa7
......@@ -100,14 +100,13 @@ math.log(3000)
1. `df["saledate"].dt.year`
1. `df_train.isnull().any().head(60)`
# Fields versus methods
# 字段 VS 方法
Objects have functions, which we call *methods* to distinguish them from functions not associated with objects. Objects also have variables, which we call *fields* or *instance variables*.
对象具有函数,我们将其称为*方法*,以将它们与不与对象关联的函数区分开来。 对象也有变量,我们称之为*字段**实例变量*
Fields are the *state* of the object. Methods are the *behavior* of the object.
We've also been using fields all along, such as `df.columns` that gets the list of columns in a data frame.
字段是对象的*状态*。 方法是对象的*行为*
我们一直在使用字段,例如`df.columns`,它获取数据帧中的列名的列表。
```python
import datetime
......@@ -123,8 +122,7 @@ print( now.month )
'''
```
If you try to access an objects function without the parentheses, the expression evaluates to the function object itself instead of calling it:
如果尝试在没有括号的情况下访问对象函数,则表达式将计算为函数对象本身而不是调用它:
```python
s='hi'
......@@ -133,24 +131,17 @@ s.title
# <function str.title>
```
# 简单的对象定义
类是多个对象的蓝图,通常称为*实例*。 类*封装了对象的状态和行为。
想象一下外星人在你家后院的土地上,并要求你描述一辆汽车。 您可能会描述其属性,例如轮子的数量及其功能,例如可以启动和停止。 这些是状态和行为。 通过定义它们,我们有效地定义了对象。 类名仅仅为实体命名。
按照惯例,类名称应该像“Point”一样大写。
## 作为对象替代品的元组
# A simple class definition
A class is a blueprint for multiple objects, often called *instances*. The class *encapsulates* the state and behavior of an object.
Imagine an alien lands in your backyard and asks you to describe a car. You would probably describe its attributes, such as the number of wheels, and its functionality, such as can start and stop. These are the state and behavior. By defining them, we effectively define the object. The class name is just giving a name to the entity.
By convention, class names should be capitalized like `Point`.
## Tuples as object substitutes
The fields of an object are the data items we want to associate together. For example, if I want to track book titles/authors, I can use a list of tuples:
对象的字段是我们想要关联在一起的数据项。 例如,如果我想跟踪书名/作者,我可以使用元组列表:
```python
from lolviz import *
......@@ -162,14 +153,8 @@ books = [
objviz(books)
```
![svg](img/2.8_OO_26_0.svg)
```python
for b in books:
print(f"{b[1]}: {b[0]}")
......@@ -180,7 +165,6 @@ David Brin: Startide Rising
'''
```
```python
# Or, more fancy
for title, author in books:
......@@ -192,12 +176,11 @@ David Brin: Startide Rising
'''
```
To access the elements of the tuple in both cases, we have to keep track of the order in our heads. In other words, we have to access the tuple elements like they are list elements, which they are.
为了在两种情况下访问元组的元素,我们必须跟踪我们头脑中的顺序。 换句话说,我们必须访问元组元素,就像它们是列表元素一样。
## Formal objects
A better way is to formally declare that author and title data elements should be encapsulated into a single entity called a book. Python has what I consider an extremely quirky specification but it is extremely flexible. For example, we can define an object that has no methods and no fields but then can add fields dynamically with assignment statements:
## 形式对象
更好的方法是正式声明,作者和标题数据元素应该封装到称为书籍的单个实体中。我认为 Python 的规范非常古怪,但它非常灵活。 例如,我们可以定义一个没有方法没有字段的对象,但是可以使用赋值语句动态添加字段:
```python
class Book:
......@@ -217,15 +200,9 @@ Gridlinked Neal Asher
'''
```
![svg](img/2.8_OO_31_1.svg)
But this doesn't let us define methods associated with that object (easily). Let's take a look at our first real class definition that contains a function called a *constructor*.
但这并不能让我们定义与该对象相关的方法(很容易)。让我们看看我们的第一个真正的类定义,它包含一个名为*构造器*的函数。
```python
class Book:
......@@ -235,12 +212,11 @@ class Book:
self.chapters = []
```
The constructor typically sets initial and default field values based upon the arguments.
All methods, functions defined within an object, must have an explicit first argument called `self`. This is the object under consideration.
构造器通常根据参数设置初始和默认字段值。
Then we can make a list of book objects or instances of class `Book` using instance creation syntax `Book(...,...)`:
在对象中定义的所有方法,函数必须有一个名为`self`的显式第一个参数。 这是正在考虑的对象。
然后我们可以使用实例创建语法`Book(..., ...)`来创建一列`Book`类的书籍对象或实例:
```python
books = [
......@@ -249,19 +225,12 @@ books = [
]
```
```python
objviz(books)
```
![svg](img/2.8_OO_36_0.svg)
```python
for b in books:
print(f"{b.author}: {b.title}") # access fields
......@@ -272,12 +241,11 @@ Startide Rising: David Brin
'''
```
Notice that we do not pass the `self` parameter to the constructor. **It's implicit at the call site but explicit at the definition site!**
请注意,我们不会将`self`参数传递给构造函数。 **它在调用一侧隐藏,但在定义一侧出现!**
## Naughty behavior
Also notice that we have been setting fields of objects using the constructor, by Python in its infinite flexibility allows you to do very **naughty** things such as setting fields on arbitrary objects:
## 顽皮的行为
还要注意我们一直在使用构造函数设置对象的字段,Python 以其无限的灵活性允许你做非常顽皮的事情,比如在任意对象上设置字段:
```python
class Foo:
......@@ -287,14 +255,13 @@ x = Foo()
x.foo = 3
```
That does not get an error even though the class itself does not define foo!
You can even [add methods on the fly](https://stackoverflow.com/questions/972/adding-a-method-to-an-existing-object-instance).
即使类本身没有定义`foo`,也不会出错!
# Defining methods
您甚至可以[动态添加方法](https://stackoverflow.com/questions/972/adding-a-method-to-an-existing-object-instance)
If you try to print out a book you will see just the type information and the physical memory address:
# 定义方法
如果您尝试打印一本书,您将只看到类型信息和物理内存地址:
```python
print(books[0])
......@@ -302,8 +269,6 @@ print(books[0])
# <__main__.Book object at 0x115c51eb8>
```
```python
class Book:
def __init__(self, title, author):
......@@ -322,7 +287,6 @@ books = [
]
```
```python
print(books[0]) # calls __str__()
books[0] # calls __repr__()
......@@ -334,11 +298,7 @@ Book(Gridlinked, Neal Asher)
'''
```
Make sure that you use `self.x` to refer to field `x`, otherwise you are creating a local variable inside a method:
确保使用`self.x`来引用字段`x`,否则你在方法中创建一个局部变量:
```python
class Foo:
......@@ -348,8 +308,7 @@ class Foo:
x = 3 # WARNING: does not alter the field! should be self.x
```
Let's create another method that sets the count of book sold.
让我们创建另一种设置销售图书数量的方法。
```python
class Book:
......@@ -381,7 +340,7 @@ Book(Gridlinked, Neal Asher, sold=100)
'''
```
**Note**: that from *within* a method definition, we call other methods on the same object using `self.foo(...)` for method `foo`.
**注意**:在方法定义中,我们调用同一对象上的其他方法,使用`self.foo(...)`调用方法`foo`
## The key to understanding methods versus functions
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册