Python学习系列(五)
本节主要学习Python 3.1的复杂数据类型,内存管理和模块,下面做了一个Sample:'''Created on 2010-4-5@author: Jamson Huang'''import os#import filecmpimport copy#Python常用的module: os#pdb=>调试器#logging=>记录器#profile,hotshot,cProfile:性能调试器#所有类的基类都是Objectif __name__ == '__main__': #global zone tempStr = 'start to learn python class' intA = 5 intB = 6 #type():the type of all objects is 'type' def typeFunc(): print(type('42')) print(type(type('42'))) print() typeFunc() #NoneType/None(Python 3.1)<=>Null(Python 2.6) def noneFunc(): print(type(None)) if (None): print('None is False') else: print('None is not True') a = 'adajkd' #两个对象之间的比较 if (a is None): print('a is None') else: print('a is not None') noneFunc() def moduleFunc(): print(os.name) moduleFunc() #CALL self-definiton class# policy = Policy() # policy.setPolicyId('1111111')# print(policy.getPolicyId()) #Python的内存管理:同java语言,程序员本身不需要关心它。由于python采用引用计数的方式,所以垃圾收集器 #会回收那些计数器为0的变量和类,当然,也可以用del来直接做回收处理。其实,这点同java类似。 #Seqence def seqFunc(): print(tempStr[::-1]) print(tempStr[::-2]) seqFunc() #内建函数:cmp(obj1,obj2)/str(obj)/repr(obj),'obj'/type()/isinstance()# print(cmp(str(intA), str(intB))) #工厂函数:file()/dict()/bool()/tuple()/basestring()/unicode() print(str(tempStr)) #id():变量和实例在内存中的标识符 #存储模型:1.标量/原子类型(数值,字符串)2.容器模型(列表,元组,字典) #更新模型: 1.变量类型:列表,字典;2.不可变类型:数值,字符串,元组 #访问模型: 1.直接访问:数字; 2.顺序访问:字符串,列表,元组;3.映射访问:字典 def complexTypeFunc(): print(id(tempStr)) aList = ['china', 'shanghai'] print(id(aList)) aList.append('Python') print(aList) print(id(aList)) #列表/元组类型:列表是可变类型,而元组是不可变类型 aList.append(['3333','4444']) print(aList) print(aList) aTuple = ('china','shanghai',['aaaa','bbbb']) print(aTuple) print(id(aTuple)) print(aTuple.index('shanghai')) print(aTuple[::1]) print(aTuple[:1]) print(aTuple) #copy和deepCopy:deepCopy会完全Create一个新的容器类型。 newTuple = copy.copy(aTuple) print('Copy:', newTuple) print(id(newTuple)) newDeepTuple = copy.deepcopy(aTuple) print('deepCopy:', newDeepTuple) print(id(newDeepTuple)) complexTypeFunc()
run Python, Console输出如下:
<class 'str'><class 'type'><class 'NoneType'>None is not Truea is not Nonentssalc nohtyp nrael ot tratssacnhy re ttasstart to learn python class1161078412877952['china', 'shanghai', 'Python']12877952['china', 'shanghai', 'Python', ['3333', '4444']]['3333', '4444']('china', 'shanghai', ['aaaa', 'bbbb'])128681041('china', 'shanghai', ['aaaa', 'bbbb'])('china',)shanghaiCopy: ('china', 'shanghai', ['aaaa', 'bbbb'])12868104deepCopy: ('china', 'shanghai', ['aaaa', 'bbbb'])12878432
页:
[1]