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

170 lines
1.0 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.
# 什么是对象?
`Python` 中,几乎所有的东西都是对象。
整数是对象:
In [1]:
```py
a = 257
```
In [2]:
```py
type(a)
```
Out[2]:
```py
int
```
In [3]:
```py
id(a)
```
Out[3]:
```py
53187032L
```
`b``a` 是同一个对象:
In [4]:
```py
b = a
```
In [5]:
```py
id(b)
```
Out[5]:
```py
53187032L
```
In [6]:
```py
c = 258
id(c)
```
Out[6]:
```py
53186960L
```
函数:
In [7]:
```py
def foo():
print 'hi'
```
In [8]:
```py
type(foo)
```
Out[8]:
```py
function
```
In [9]:
```py
id(foo)
```
Out[9]:
```py
63632664L
```
`type` 函数本身也是对象:
In [10]:
```py
type(type)
```
Out[10]:
```py
type
```
In [11]:
```py
id(type)
```
Out[11]:
```py
506070640L
```
只有一些保留的关键词不是对象:
In [12]:
```py
id(if)
```
```py
File "<ipython-input-12-1e0d1307109a>", line 1
id(if)
^
SyntaxError: invalid syntax
```
In [13]:
```py
id(+)
```
```py
File "<ipython-input-13-86853fe3c6fd>", line 1
id(+)
^
SyntaxError: invalid syntax
```