1.如果在一个方法中有yield关键字则该方法返回的是一个生成器对象 2.对生成器对象进行操作必须进行迭代或循环处理 例如: yield_test.py #!/usr/bin/env python # _*_ coding:UTF-8 _*_ def MyReadLines(): seek = 0 while True: with open('myFile.txt', 'rb') as f: f.seek(seek) line = f.readline() if line: seek = f.t…
1.常见的错误 TypeError 类型错误 NameError 没有该变量 ValueError 不期望的值 AttributeError 没有该属性 UnboundLocalError 没有该局部变量 ImportError 没有该模块 IOError 打不开文件 IndexError 列表没有该下标 KeyError 字典没有该键 IndentationError 代码没有对齐 SyntaxError 语法错误 KeyboardError Ctrl+C被按下 2.示例代码 #!/usr/b…
1. 体验多进程的运行速度 #!/usr/bin/env python # _*_ coding:UTF-8 _*_ from multiprocessing import Pool import time def foo(n): time.sleep(1) return n * n if __name__ == "__main__": pool = Pool(10) data_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # 这里只需要等待1S就能得到…
1. 线程的创建与运行 #!/usr/bin/env python # _*_ coding:UTF-8 _*_ from threading import Thread def foo(param1, param2): print "{0}{1}".format(param1, param2) if __name__ == "__main__": print "main thread running" thread = Thread(targe…
1.使用socket实现文件上传 server.py #!/usr/bin/env python # _*_ coding:UTF-8 _*_ import os import SocketServer class MyServer(SocketServer.BaseRequestHandler): def handle(self): base_path = "/Users/liudaoqiang/" conn = self.request print "connected&…
1.类与对象(构造方法与实例化) #!/usr/bin/env python # _*_ coding:UTF-8 _*_ class Province: def __init__(self, name, capital, leader): self.name = name self.capital = capital self.leader = leader if __name__ == "__main__": hebei = Province("河北", &qu…
装饰器:在某个方法执行前后去执行其他新定义的行为 例如: #!/usr/bin/env python # _*_ coding:UTF-8 _*_ def before_say_hello(): print "before hello" def after_say_hello(): print "after hello" def say_hello_wrapper(func): def wrapper(): print "Before" befo…
例子: sys.argv 命令行参数List,第一个元素是程序本身路径 sys.exit(n) 退出程序,正常退出时exit(0) sys.version 获取Python解释程序的版本信息 sys.maxint 最大的Int值 sys.maxunicode 最大的Unicode值 sys.path 返回模块的搜索路径,初始化时使用PYTHONPATH环境变量的值 sys.platform 返回操作系统平台名称 sys.stdout.write('please:') val = sys.stdi…
例如: #!/usr/bin/env python # _*_ coding:UTF-8 _*_ import time if __name__ == "__main__": print time.time() //获取当前时间戳 print time.mktime(time.localtime()) //将结构化时间对象转化为时间戳 print time.localtime() //将时间戳转化为机构化时间对象,默认传入当前时间戳 print time.gmtime() //将时间戳…
序列化是使用二进制的方式加密列表,字典或集合,反序列化是解密的过程:序列化开启了两个独立进程进行数据交互的通路 使用pickle进行序列化和反序列化 例如: pickle_test.py #!/usr/bin/env python # _*_ coding:UTF-8 _*_ import pickle if __name__ == "__main__": name_list = ["liudaoqiang", 11, 22, "success"…