ch06动态类型简介(python)
>>> a = 3
#python的动态类型
>>> a = 3 # It's an integer
>>> a = 'spam' # Now it's a string
>>> a = 1.23 # Now it's a floating point
>>> a = 3
>>> a = 'spam'
#python的对象有引用计数,当该计数为0的时候,垃圾回收机制收回该对象占用的内存空间
>>> x = 42
>>> x = 'shrubbery' # Reclaim 42 now (unless referenced elsewhere)
>>> x = 3.1415 # Reclaim 'shrubbery' now
>>> x = [1, 2, 3] # Reclaim 3.1415 now
#python的引用
>>> a = 3
>>> b = a
>>> a = 'spam'
#int类型是不可变的
>>> a = 3
>>> b = a
>>> a = a + 2
#list是可变的
>>> L1 = [2, 3, 4]
>>> L2 = L1
>>> L1 = 24
#L1和L2指向同一个内存对象
>>> L1 = [2, 3, 4] # A mutable object
>>> L2 = L1 # Make a reference to the same object
>>> L1[0] = 24 # An in-place change
>>> L1 # L1 is different
[24, 3, 4]
>>> L2 # But so is L2!
[24, 3, 4]
#L2只是L1的一个副本
>>> L1 = [2, 3, 4]
>>> L2 = L1[:] # Make a copy of L1
>>> L1[0] = 24
>>> L1
[24, 3, 4]
>>> L2 # L2 is not changed
[2, 3, 4]
#python的复制
import copy
X = copy.copy(Y) # Make top-level "shallow" copy of any object Y
X = copy.deepcopy(Y) # Make deep copy of any object Y: copy all nested parts
>>> x = 42
>>> x = 'shrubbery' # Reclaim 42 now
>>> L = [1, 2, 3]
>>> M = L # M and L reference the same object
>>> L == M # Same value,比较的是内容
True
>>> L is M # Same object,比较是否为同一个对象
True
is判断是否指向同一个对象
>>> L = [1, 2, 3]
>>> M = [1, 2, 3] # M and L reference different objects
>>> L == M # Same values
True
>>> L is M # Different objects
False
#python的int和str有优化,有缓存
>>> X = 42
>>> Y = 42 # Should be two different objects
>>> X == Y
True
>>> X is Y # Same object anyhow: caching at work!
True
#获取引用次数
>>> import sys
>>> sys.getrefcount(1) # 837 pointers to this shared piece of memory
837
A = "spam"
B = A
B = "shrubbery"
A = ["spam"]
B = A
B[0] = "shrubbery"
A = ["spam"]
B = A[:]
B[0] = "shrubbery"
python类型属于对象,不属于变量
动态类型也是python动态的根本
如果使用正确的话,动态类型和多态产生的代码,
可以自动的适应系统的新需求
python只有这一种赋值模型,引用
#python的动态类型
>>> a = 3 # It's an integer
>>> a = 'spam' # Now it's a string
>>> a = 1.23 # Now it's a floating point
>>> a = 3
>>> a = 'spam'
#python的对象有引用计数,当该计数为0的时候,垃圾回收机制收回该对象占用的内存空间
>>> x = 42
>>> x = 'shrubbery' # Reclaim 42 now (unless referenced elsewhere)
>>> x = 3.1415 # Reclaim 'shrubbery' now
>>> x = [1, 2, 3] # Reclaim 3.1415 now
#python的引用
>>> a = 3
>>> b = a
>>> a = 'spam'
#int类型是不可变的
>>> a = 3
>>> b = a
>>> a = a + 2
#list是可变的
>>> L1 = [2, 3, 4]
>>> L2 = L1
>>> L1 = 24
#L1和L2指向同一个内存对象
>>> L1 = [2, 3, 4] # A mutable object
>>> L2 = L1 # Make a reference to the same object
>>> L1[0] = 24 # An in-place change
>>> L1 # L1 is different
[24, 3, 4]
>>> L2 # But so is L2!
[24, 3, 4]
#L2只是L1的一个副本
>>> L1 = [2, 3, 4]
>>> L2 = L1[:] # Make a copy of L1
>>> L1[0] = 24
>>> L1
[24, 3, 4]
>>> L2 # L2 is not changed
[2, 3, 4]
#python的复制
import copy
X = copy.copy(Y) # Make top-level "shallow" copy of any object Y
X = copy.deepcopy(Y) # Make deep copy of any object Y: copy all nested parts
>>> x = 42
>>> x = 'shrubbery' # Reclaim 42 now
>>> L = [1, 2, 3]
>>> M = L # M and L reference the same object
>>> L == M # Same value,比较的是内容
True
>>> L is M # Same object,比较是否为同一个对象
True
is判断是否指向同一个对象
>>> L = [1, 2, 3]
>>> M = [1, 2, 3] # M and L reference different objects
>>> L == M # Same values
True
>>> L is M # Different objects
False
#python的int和str有优化,有缓存
>>> X = 42
>>> Y = 42 # Should be two different objects
>>> X == Y
True
>>> X is Y # Same object anyhow: caching at work!
True
#获取引用次数
>>> import sys
>>> sys.getrefcount(1) # 837 pointers to this shared piece of memory
837
A = "spam"
B = A
B = "shrubbery"
A = ["spam"]
B = A
B[0] = "shrubbery"
A = ["spam"]
B = A[:]
B[0] = "shrubbery"
python类型属于对象,不属于变量
动态类型也是python动态的根本
如果使用正确的话,动态类型和多态产生的代码,
可以自动的适应系统的新需求
python只有这一种赋值模型,引用
评论
发表评论