05 2008 档案

摘要: __getattr__和__setattr__可以用来对属性的设置和取值进行处理,比如下面的例子:[代码]上面的例子中,在赋值书的value属性时,偷偷的将value减去了100,呵。输出结果:Python0Python cost : 0Type is not found!Python 天天美味系列(总)Python 天天美味(24) - 初始化多维数组 Python 天天美味(25) - 深入...阅读全文
posted @ 2008-05-25 15:39 CoderZh 阅读(995) | 评论 (14) 编辑
摘要: yield的英文单词意思是生产,刚接触Python的时候感到非常困惑,一直没弄明白yield的用法。本人将深入讨论Python中yield的用法。后来发现.Net也有yield关键字,恩,学习了~阅读全文
posted @ 2008-05-18 17:01 CoderZh 阅读(4591) | 评论 (7) 编辑
摘要: Python中初始化一个5 x 3每项为0的数组,最好方法是:Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->multilist=[[0forcolinrange(5)]forrowinrange(3)]我们知道,为了初始化一个一维数组,我们可以这样做...阅读全文
posted @ 2008-05-18 14:40 CoderZh 阅读(1913) | 评论 (6) 编辑
摘要: 其他语言中,比如C#,我们通常遍历数组是的方法是:Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->for(inti = 0; i < list.Length; i++){//todo with list[i]}在Python中,我们习惯这样遍历:...阅读全文
posted @ 2008-05-17 20:04 CoderZh 阅读(804) | 评论 (2) 编辑
摘要: Python中的对象之间赋值时是按引用传递的,如果需要拷贝对象,需要使用标准库中的copy模块。1. copy.copy 浅拷贝 只拷贝父对象,不会拷贝对象的内部的子对象。2. copy.deepcopy 深拷贝 拷贝对象及其子对象一个很好的例子:Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.C...阅读全文
posted @ 2008-05-17 19:49 CoderZh 阅读(1412) | 评论 (6) 编辑
摘要: 例一:使用httplib访问某个url然后获取返回的内容:Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->importhttplibconn=httplib.HTTPConnection("www.cnblogs.com")conn.request("...阅读全文
posted @ 2008-05-17 17:58 CoderZh 阅读(1130) | 评论 (1) 编辑
posted @ 2008-05-17 11:59 CoderZh 阅读(495) | 评论 (0) 编辑
摘要: 命令行参数是通过sys.argv[]来获取的,sys.argv[0]是代码文件本身的路径,因此参数是从1开始的。比如设置参数为: spePython代码:Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->importos,sysos.system(sys...阅读全文
posted @ 2008-05-16 19:31 CoderZh 阅读(2750) | 评论 (1) 编辑
摘要: Python Cook书中有很多章节都是针对某个库的使用进行介绍或是通过组合多个函数实现一些复杂的功能。我这里直接跳过了上一章节中对于文件处理的一些章节,直接进入对时间操作的章节。同时,关于datetime也是简单介绍。因为有很多东西需要自己去使用,去查帮助才最有效。例子:计算上一个星期五并输出。解答:Code highlighting produced by Actipro CodeHighli...阅读全文
posted @ 2008-05-16 19:19 CoderZh 阅读(1098) | 评论 (0) 编辑
posted @ 2008-05-16 16:49 CoderZh 阅读(381) | 评论 (7) 编辑
摘要: Python中使用标准库中的linecache中的getline方法可以从某个文件中读取出特定的一行。比如:Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->importlinecacheprintlinecache.getline('2.1_open....阅读全文
posted @ 2008-05-10 21:52 CoderZh 阅读(1994) | 评论 (0) 编辑
摘要: Python中文件操作可以通过open函数,这的确很像C语言中的fopen。通过open函数获取一个file object,然后调用read(),write()等方法对文件进行读写操作。1.open使用open打开文件后一定要记得调用文件对象的close()方法。比如可以用try/finally语句来确保最后能关闭文件。Code highlighting produced by Actipro C...阅读全文
posted @ 2008-05-10 17:32 CoderZh 阅读(2836) | 评论 (4) 编辑
摘要: Python中的map函数非常有用,在字符转换和字符遍历两节都出现过,现在,它又出现了,会给我们带来什么样的惊喜呢?是不是要告诉我们,map是非常棒的,以后要多找它玩呢?具体的实例我们需要在目录中遍历,包括子目录(哈哈),找出所有后缀为:rmvb ,avi ,pmp 的文件。(天哪?!你要干什么?这可是我的隐私啊~~)Code highlighting produced by Actipro Co...阅读全文
posted @ 2008-05-09 16:22 CoderZh 阅读(1245) | 评论 (7) 编辑
摘要: 找到一篇专门讲Python中的正则表达式的文章,写的非常全面,转过来学习。阅读全文
posted @ 2008-05-06 19:54 CoderZh 阅读(5024) | 评论 (8) 编辑
摘要: Python中的splitlines用来分割行。当传入的参数为True时,表示保留换行符 \n。通过下面的例子就很明白了:Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->mulLine="""Hello!!!WellcometoPython'sworl...阅读全文
posted @ 2008-05-05 20:05 CoderZh 阅读(798) | 评论 (0) 编辑
摘要: Python中按一定的格式取出某字符串中的子字符串,使用struck.unpack是非常高效的。1. 设置fomat格式,如下:Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->#取前5个字符,跳过4个字符,再取3个字符format='5s4x3s'2....阅读全文
posted @ 2008-05-04 11:29 CoderZh 阅读(2503) | 评论 (12) 编辑
摘要: 开始以为Python中没有像其他语言一样的条件判断的缩写形式:Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->return(1==1)?"is easy":"my god"//C#中的用法其实,在Python中,是这样写的:Code highlight...阅读全文
posted @ 2008-05-04 11:08 CoderZh 阅读(1138) | 评论 (8) 编辑
摘要: 转换大小写和其他语言一样,Python为string对象提供了转换大小写的方法:upper() 和 lower()。还不止这些,Python还为我们提供了首字母大写,其余小写的capitalize()方法,以及所有单词首字母大写,其余小写的title()方法。函数较简单,看下面的例子:Code highlighting produced by Actipro CodeHighlighter (fr...阅读全文
posted @ 2008-05-04 10:32 CoderZh 阅读(2792) | 评论 (16) 编辑
摘要: Python中将两个整数相除,默认结果是为整数的。但我们可以通过下面的方法,使得两个整数相除的结果为小数。Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->from__future__importdivisionprint7/3输出结果:2.333333...阅读全文
posted @ 2008-05-04 09:55 CoderZh 阅读(940) | 评论 (8) 编辑
摘要: Python的translate方法真是太有意思了阅读全文
posted @ 2008-05-03 22:10 CoderZh 阅读(2473) | 评论 (10) 编辑
摘要: 方法一,使用[::-1]:Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->s='python'prints[::-1]方法二,使用reverse()方法:Code highlighting produced by Actipro CodeHighlig...阅读全文
posted @ 2008-05-03 20:43 CoderZh 阅读(744) | 评论 (1) 编辑
摘要: join 方法用于连接字符串数组Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->s=['a','b','c','d']print''.join(s)print'-'.join(s)输出结果:abcda-b-c-d使用 % 连接多个变量Code high...阅读全文
posted @ 2008-05-03 20:27 CoderZh 阅读(1409) | 评论 (3) 编辑
摘要: Python中的strip用于去除字符串的首位字符,同理,lstrip用于去除左边的字符,rstrip用于去除右边的字符。这三个函数都可传入一个参数,指定要去除的首尾字符。注意的是,传入的是一个字符数组,编译器去除两端所有相应的字符,直到没有匹配的字符,比如:Code highlighting produced by Actipro CodeHighlighter (freeware)http:/...阅读全文
posted @ 2008-05-02 19:11 CoderZh 阅读(1324) | 评论 (8) 编辑
摘要: Python中打印字符串时可以调用ljust(左对齐),rjust(右对齐),center(中间对齐)来输出整齐美观的字符串,使用起来非常简单,包括使用第二个参数填充(默认为空格)。看下面的例子就会明白了:Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--...阅读全文
posted @ 2008-05-02 18:36 CoderZh 阅读(964) | 评论 (4) 编辑
摘要: Python中判度对象类型方法非常简单,不需要像别的语言一样使用如下的判断方法:Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->if(typeof(objA)==typeof(String)){//TODO}在Python中只需要使用内置的函数isin...阅读全文
posted @ 2008-05-02 16:47 CoderZh 阅读(1464) | 评论 (1) 编辑
摘要: Python提供了ord和chr两个内置的函数,用于字符与ASCII码之间的转换。如:Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->>>>printord('a')97>>>printchr(97)a下面我们可以开...阅读全文
posted @ 2008-05-02 16:24 CoderZh 阅读(1327) | 评论 (3) 编辑