个人记录
深copy 使用
In [1]: a = [11,22,33]
In [2]: b = a
In [3]: a == b
Out[3]: True
In [4]: a is b
Out[4]: True
In [5]: id(a)
Out[5]: 24839808
In [6]: id(b)
Out[6]: 24839808
In [7]: import copy
In [8]: b = ['a','b','c']
In [9]: a
Out[9]: [11, 22, 33]
In [10]: b
Out[10]: ['a', 'b', 'c']
In [11]: c = copy.deepcopy(b)
In [12]: b
Out[12]: ['a', 'b', 'c']
In [13]: c
Out[13]: ['a', 'b', 'c']
In [14]: id(b)
Out[14]: 24829392
In [15]: id(c)
Out[15]: 22442784
In [16]: b = c
In [17]: c = copy.deepcopy(b)
In [18]: b == c
Out[18]: True
In [19]: b is c
Out[19]: False
In [20]: id(b)
Out[20]: 22442784
In [21]: id(c)
Out[21]: 22443000
In [22]: b
Out[22]: ['a', 'b', 'c']
In [23]: c
Out[23]: ['a', 'b', 'c']
In [24]: b.append(666)
In [25]: b
Out[25]: ['a', 'b', 'c', 666]
In [26]: c
Out[26]: ['a', 'b', 'c']
In [28]: a = [11,22,33]
In [29]: b = [44,55,66]
In [30]: c = [a,b]
In [31]: d = copy.deepcopy(c)
In [32]: d
Out[32]: [[11, 22, 33], [44, 55, 66]]
In [33]: c
Out[33]: [[11, 22, 33], [44, 55, 66]]
In [34]: id(d)
Out[34]: 24789216
In [35]: id(c)
Out[35]: 24789648
In [36]: d == c
Out[36]: True
In [37]: d is c
Out[37]: False
In [38]: a.append(666)
In [39]: d
Out[39]: [[11, 22, 33], [44, 55, 66]]
In [40]: c
Out[40]: [[11, 22, 33, 666], [44, 55, 66]]
In [41]: a = [1,2,3]
In [42]: b = [4,5,6]
In [43]: c = (a,b)
In [44]: d = copy
copy copyright
In [44]: d = copy.deepcopy(c)
In [45]: d
Out[45]: ([1, 2, 3], [4, 5, 6])
In [46]: c
Out[46]: ([1, 2, 3], [4, 5, 6])
In [47]: d == c
Out[47]: True
In [48]: d is c
Out[48]: False
In [49]: id(d)
Out[49]: 23815592
In [50]: id(c)
Out[50]: 23799784
In [51]: a.append('abcd')
In [52]: d
Out[52]: ([1, 2, 3], [4, 5, 6])
In [53]: c
Out[53]: ([1, 2, 3, 'abcd'], [4, 5, 6])
In [54]: d == c
Out[54]: False
In [55]: a
Out[55]: [1, 2, 3, 'abcd']
In [56]: b
Out[56]: [4, 5, 6]
In [57]: e = a
In [58]: id(e)
Out[58]: 24783904
In [59]: id(a)
Out[59]: 24783904
In [60]: a is e
Out[60]: True
In [61]: a == e
Out[61]: True
In [62]:
```
使用装饰器方法
class project():
def __init__(self):
self.__num = 100
@property
def num(self, value):
self.__num = value
@num.setter
def num(self):
print self.__num
cc = project()
cc.num = 500
print cc.num
class project():
def __init__(self):
self.__num = 100
def setNum(self, value):
self.__num = value
def getNum(self):
print self.__num
num = property(setNum, getNum)
cc = project()
cc.getNum() # 获取初始化值
cc.setNum(22) # 自己设定一个值
cc.getNum() # 再次获取自定义值
print '-' * 40
# 使用property 方法
cc.num = 999
print cc.num
# 两种法法 使用哪种法法都可以
装饰器记录
def func(fun):
def inner():
print '__排队__'
fun()
print '__通过__'
return inner
@func
def f1():
print '1'
# fc = func(f1)
# fc()
f1()
# 将一个函数扔到另一函数执行,不改变原来函数
后续还会记录...
本文出自 岳贺庚 51cto博客,随意转载!