Python 数据是对象
对象思想
Python 程序中的一切数据都是对象。对象包括自定义对象及基本的数据类型(如数值、字符串、列表、字典)等。
# coding=utf-8
__author__ = 'Z'
x = 1
y = 2
print('x:', x, id(x))
print('y:', y, id(y))
y = x
print('x:', x, id(x))
print('y:', y, id(y))
x = 5
print('x:', x, id(x))
print('y:', y, id(y))
运行结果:
('x:', 1, 30801560)
('y:', 2, 30801548)
('x:', 1, 30801560)
('y:', 1, 30801560)
('x:', 5, 30801512)
('y:', 1, 30801560)
Python中皆对象,参见官方文档:
Assignments do not copy data — they just bind names to objects. The same is true for deletions: the statement
del x
removes the binding ofx
from the namespace referenced by the local scope.
附:id()
函数
id(…) 返回对象的内存地址
id(object) -> integer
Return the identity of an object. This is guaranteed to be unique among simultaneously existing objects. (Hint: it’s the object’s memory address.)