python decorator
1.常用方法,不带参数
def decator(func):
def inner_func(*args):
args = (i * 2 for i in args)
return func(*args)
return inner_func
@decator
def add(a, b, c):
return a + b + c
2.带参数:
class Dec(object):
def __init__(self, i):
self.i = i
def __call__(self, func):
def inner_func(a, b, c):
return self.i * func(a, b, c)
return inner_func
@Dec(2)
def abc(x, y, z):
print "function abc"
print (x, y, z)
return x + y + z
相关文档:
对数据库的操作基本分为三步:
连接数据库
根据需要执行SQL语句,接受返回值
关闭连接
我们正常的数据库应该都离不开这三步,下来说说如何使用python中的MySQLdb模块进行这些操作:
首先,我们需要把MySQLdb引入到程序中
import MySQLdb
然后开始数据库操作
1.数据库连接
conn = MySQLdb. ......
#
-*- encoding: gb2312 -*-
import
os, sys, string
import
MySQLdb
#
连接数据库
try
:
conn
=
MySQLdb.connect(host
=
'
localhost
'
,user
=
'
root
'
,passwd
=
'
xxxx
'
,db
=
'
test1
'
)
except
Exception, e:
print
e
sys.exit()
......
8.8. queue — A synchronized queue class¶
queue -- 一个同步队列类
The queue module implements multi-producer, multi-consumer queues. It is especially useful in threaded programming when information must be exchanged safely between multiple threads. The Queue class in this module implemen ......
原帖:http://www.cnblogs.com/jingleguo/archive/2008/06/02/1211820.html
当python中间处理非ASCII编码时,经常会出现如下错误:
UnicodeDecodeError: 'ascii' codec can't decode byte 0x?? in position 1: ordinal not in range(128)
0x??是超出128的数字,python在默认的情况下认为语言的编码是ascii编码,所以无法 ......