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