随笔分类 - Python
摘要:class C: str = {} def __init__(self): self.d = D() self.name = "sophia tang" def f(self): self.d.f1(self.str) print "self.str==",self.strclass D: def f1(self, str): str['a'] = 'hello' self.f2(str) import inspect caller = inspect.stack()[1][0].f_locals['self
阅读全文
摘要:TODO
阅读全文
posted @ 2011-09-23 14:02
SophiaTang
摘要:Python中的splitlines用来分割行。当传入的参数为True时,表示保留换行符 \n。通过下面的例子就很明白了代码如下:mulLine = """Hello!!! Wellcome to Python's world! There are a lot of interesting things! Enjoy yourself. Thank you!"""print ''.join(mulLine.splitlines()) print '------------' print '
阅读全文
posted @ 2011-09-23 13:39
SophiaTang
摘要:python不支持直接的引用参数传递,因为python可以一次返回多个值,所以这个问题在python提得不多。可以传入可变参数,然后在函数中进行修改,如list, dict, object等。举个list的例子:def p(a): #假定a是一个list a.append(1)t = []p(t)这样t在调用了p()之后由[]变成了[1]。t本身没有变,但它的内容变化了。这就是引用类似的例子。原理就是传入的对象的值可以变化,因此与C中的指针类型。在python中可变类型有list, dict, object。
阅读全文
posted @ 2011-09-23 11:52
SophiaTang
摘要:形参赋值的过程是这样的:首先按顺序把“arg”这种形式的实参给对应的形参第二,把“arg=”这种形式的实参赋值给形式第三,把多出来的“arg”这种形式的实参组成一个tuple给带一个星号的形参第四,把多出来的“key=value”这种形式的实参转为一个dictionary给带两个星号的形参。听起来好复杂,实际是是很简单的。很直观,来看例子: 1. def test(x,y=5,*a,**b): 2. print x,y,a,b 就这么一个简单函数,来看看下面对这个函数调用会产生什么结果: test(1) ===> 1 5 () {} test(1,2) ===> 1 2 () {}
阅读全文
posted @ 2011-09-23 11:34
SophiaTang
摘要:ConfigParser – Work with configuration files Purpose:Read/write configuration files similar to Windows INI filesAvailable In:1.5Use the ConfigParser module to manage user-editable configuration files for an application. The configuration files are organized into sections, and each section can conta.
阅读全文
posted @ 2011-09-22 17:03
SophiaTang
摘要:python两个dict相加>>> a = {'1':'2'}>>> b = {'3':'4'}>>> a+bTraceback (most recent call last):File "<stdin>", line 1, in <module>TypeError: unsupported operand type(s) for +: 'dict' and 'dict'>>> a.u
阅读全文
posted @ 2011-09-21 16:58
SophiaTang
摘要:udo apt-get install python-mysqldb安装完成之后可以在Python解释器中测试一下输入Python代码importMySQLdb#注意大小写!!import MySQLdb #注意大小写!!如果不报错,就证明安装成功了,可能继续了MySQLdb在Python中也就相当于JAVA中的MySQL的JDBC Driver,Python也有类似的数据接口规范Python DB API,MySQLdb就是Mysql的实现。操作也比较简单和其它平台或语言操作数据库一样,就是建立和数据库系统的连接,然后给数据库输入SQL,再 从数据库获取结果。先写一个最简单的,创建一个数据库
阅读全文
posted @ 2011-09-21 14:14
SophiaTang
摘要:在linux的一些bash的脚本,需在开头一行指定脚本的解释程序,如:#!/usr/bin/env python再如:#!/usr/bin/env perl#!/usr/bin/env zimbu但有时候也用#!/usr/bin/python和#!/usr/bin/perl那么 env到底有什么用?何时用这个呢?脚本用env启动的原因,是因为脚本解释器在linux中可能被安装于不同的目录,env可以在系统的PATH目录中查找。同时,env还规定一些系统环境变量。如我系统里env程序执行后打印结果:zhouhh@zhh64:~$ envORBIT_SOCKETDIR=/tmp/orbit-zho
阅读全文
posted @ 2011-09-21 09:44
SophiaTang
摘要:脚本语言的第一行,目的就是指出,你想要你的这个文件中的代码用什么可执行程序去运行它,就这么简单#!/usr/bin/python是告诉操作系统执行这个脚本的时候,调用/usr/bin下的python解释器;#!/usr/bin/env python这种用法是为了防止操作系统用户没有将python装在默认的/usr/bin路径里。当系统看到这一行的时候,首先会到env设置里查找python的安装路径,再调用对应路径下的解释器程序完成操作。#!/usr/bin/python相当于写死了python路径;#!/usr/bin/env python会去环境设置寻找python目录,推荐这种写法
阅读全文
posted @ 2011-09-21 09:38
SophiaTang
摘要:Logging to a single file from multiple processesAlthough logging is thread-safe, and logging to a single file from multiple threads in a single process is supported, logging to a single file from multiple processes is not supported, because there is no standard way to serialize access to a single fi
阅读全文
摘要:一般情况下,一些程序的调试过程中我们会让它输出一些信息,特别是一些大型的程序,我们通过这些信息可以了解程序的运行情况,python提供了一个日志模块logging,它可以把我们想要的信息全部保存到一个日志文件中,方面我们查看。我们先看一个简单的例子。>>> import logging>>> LOG_FILENAME="C:\Python25\log_test.txt">>> logging.basicConfig(filename=LOG_FILENAME,level=logging.DEBUG)>>>
阅读全文
posted @ 2011-09-19 13:14
SophiaTang
摘要:对于 Python 来说,由于 GIL 的限制,多进程才能利用多 CPU 或 core 的架构。http://www.rainsts.net/article.asp?id=1017
阅读全文
摘要:当一般内部函数被用作变量名后可能出现此错误。比如:uyy平坦软件园range=1uyy平坦软件园foriin range(0,1):uyy平坦软件园………uyy平坦软件园就会报这样的错误uyy平坦软件园这样的错会报在for行,但是时间引起的原因却是在range=1这行,如果两行相距较远,怎很难被发现。所以要特别注意不要用内部已有的变量和函数名作自定义变量名。
阅读全文
摘要:http://www.ibm.com/developerworks/cn/linux/l-pypt/part1/index.html
阅读全文
摘要:Python列表去掉重复元素的一种方法:>>> L = [1, 2, 3, 4, 1, 2, 3, 4, 5]>>> [x for x in L if x not in locals()['_[1]']][1, 2, 3, 4, 5]解释如下:down vote Referencing a list comprehension as it is being built...You can reference a list comprehension as it is being built by the symbol '_[1]
阅读全文

浙公网安备 33010602011771号