CR的代码文本

all for learning about the world
  订阅 订阅  :: 管理

2011年8月9日

摘要: 类的特殊方法 __init__(self,...) 构造__del__(self) 析构__str__(self) print对象或str()时调用__lt__(self,other) 当使用 小于 运算符(<)的时候调用。 类似地,对于所有的运算符(+,>等等)都有特殊的方法。__getitem__(self,key) 让对象可使用索引操作,[key]__len__(self) 对序列对象使用内建的len()函数的时候调用。函数中接收元组和列表 元组和列表是用*前缀词典用**前缀lambda, exec, eval lambda,创建新的函数对象exec,执行字符串中的pytho 阅读全文

posted @ 2011-08-09 16:11 mumuliang 阅读(205) 评论(0) 推荐(0) 编辑

摘要: sys重点关注:sys.stdin、sys.stdout和sys.stderrosos.name 字符串指示你正在使用的平台。比如对于Windows,它是'nt',而对于Linux/Unix用户,它是'posix'。os.getcwd() 函数得到当前工作目录,即当前Python脚本工作的目录路径。os.getenv()和os.putenv() 函数分别用来读取和设置环境变量。os.listdir() 返回指定目录下的所有文件和目录名。os.remove() 函数用来删除一个文件。os.system() 函数用来运行shell命令。os.linesep字符串 给 阅读全文

posted @ 2011-08-09 15:51 mumuliang 阅读(225) 评论(0) 推荐(0) 编辑

摘要: try: if err: raise Exceptionexcept Exception: # do some specialelse: # no exceptionfinally: # always do 阅读全文

posted @ 2011-08-09 15:22 mumuliang 阅读(226) 评论(0) 推荐(0) 编辑

摘要: 类1. 使用关键字class2. 类的方法的第一个参数永远是self,像显式的this。 注:第一个参数的名称是随意的,也可以不叫self,但它的意义固定。3. 使用类名后加()来创建一个类对象#!/usr/bin/python#Filename:method.pyclassPerson:defsayHi(self):print'Hello,howareyou?'p=Person()p.sayHi()4. __init__方法,即构造函数classPerson:def__init__(self,name):self.name=namedefsayHi(self):print&# 阅读全文

posted @ 2011-08-09 14:30 mumuliang 阅读(234) 评论(0) 推荐(0) 编辑

摘要: 列表[]元组()词典{}1. 都可以多重嵌套2. 切片要注意list[1:3]的意义是,从1开始,3之前的元素,不包含3因此list[1:]的意义,使用同样的逻辑来说是,从1开始,到最后一个元素的后一个位置之前的元素。-_-b3. 默认引用传递mylist = shoplist # mylist仅仅是shoplist的别名newlist = shoplist[:] # newlist是shoplist的拷贝 阅读全文

posted @ 2011-08-09 12:38 mumuliang 阅读(233) 评论(2) 推荐(0) 编辑

摘要: 使用模块 1. import module 使用时modulename类似namespace2. from module import 函数和变量列表 类似using namespace的功效,不推荐。3. import module as anothername 很面熟的语法,忘记哪儿见过。给导入模块起别名。被调用模块#!/usr/bin/python#Filename:mymodule.pyimportsysdefsayhi():if__name__=='__main__':print'Hi,thisismymodulespeaking,calledbymyself 阅读全文

posted @ 2011-08-09 10:33 mumuliang 阅读(214) 评论(0) 推荐(0) 编辑