def pf_dec(f):
@functools.wraps(f)
def fn(*args, **kw):
start = time.time()
res = f(*args, **kw)
end = time.time()
print '%s %s() in : %fs' % (unit,f.__name__,end - start)
return res
return fn
return pf_dec
@functools.wraps(f)
def fn(*args, **kw):
start = time.time()
res = f(*args, **kw)
end = time.time()
print '%s %s() in : %fs' % (unit,f.__name__,end - start)
return res
return fn
return pf_dec
2016-01-08
class Person(object):
def __init__(self, name, score):
self.name = name
self.__score = score
p = Person('Bob', 59)
print p.name
try:
print p.__score
except:
print "attributeerror"
def __init__(self, name, score):
self.name = name
self.__score = score
p = Person('Bob', 59)
print p.name
try:
print p.__score
except:
print "attributeerror"
2016-01-08
def format_name(s):
return s[0].upper() + s[1:].lower()
print map(format_name, ['adam', 'LISA', 'barT'])
return s[0].upper() + s[1:].lower()
print map(format_name, ['adam', 'LISA', 'barT'])
2016-01-07
def cmp_ignore_case(s1, s2):
if s1.upper() < s2.upper():
return -1
if s1.upper() > s2.upper():
return 1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
if s1.upper() < s2.upper():
return -1
if s1.upper() > s2.upper():
return 1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2016-01-07
import math
def comp(x):
return math.sqrt(x)==int(math.sqrt(x))
print filter(comp,range(1,101))
def comp(x):
return math.sqrt(x)==int(math.sqrt(x))
print filter(comp,range(1,101))
2016-01-07
带参数的decorator函数首先是返回一个不带参数的decorator函数,接着把函数f传入decorator函数中进行装饰,再返回装饰后的函数。
2016-01-07
def calc_prod(l):
def calc():
def f(x,y):
return x*y
return reduce(f,l)
return calc
f=calc_prod([1,2,3,4])
print f()
def calc():
def f(x,y):
return x*y
return reduce(f,l)
return calc
f=calc_prod([1,2,3,4])
print f()
2016-01-07
def f(x1,x2):
u1=x1.upper()
u2=x2.upper()
if u1>u2:
return 1
if u1<u2:
return -1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'],f)
u1=x1.upper()
u2=x2.upper()
if u1>u2:
return 1
if u1<u2:
return -1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'],f)
2016-01-07
加上第三个参数:
def prod(x, y):
return x*y
print reduce(prod, [2, 4, 5, 7, 12],1)
def prod(x, y):
return x*y
print reduce(prod, [2, 4, 5, 7, 12],1)
2016-01-07
def f(x):
return x[0:1].upper()+x[1:].lower()
print map(f,['adam','LISA','barT'])
return x[0:1].upper()+x[1:].lower()
print map(f,['adam','LISA','barT'])
2016-01-07