def performance(unit):
def performance_decorator(f):
def fn(x):
print 'call'+f._name_+'()in',time().unit
return f(x)
return fn
return performance_decorator
def performance_decorator(f):
def fn(x):
print 'call'+f._name_+'()in',time().unit
return f(x)
return fn
return performance_decorator
2016-03-06
def performance(f):
def fn(x):
print 'call ' +f._name_+'()in',time()
return f(x)
return fn
def fn(x):
print 'call ' +f._name_+'()in',time()
return f(x)
return fn
2016-03-06
最新回答 / alienpro
这典型的语文老师死得早。。。。原函数的参数名称如果不是 x,比如说是a,那么用了那个装饰器之后,参数名称就变成x了,所以老师说有可能改变原函数的参数名称。
2016-03-05
解释器内部会将**kw拆分成对应的dict.
setattr()方法接受3个参数:setattr(对象,属性,属性的值)
setattr(self,k,v)相当于self.k = v
kw.iteritems()历遍字典kw的所有key和value,分别匹配k,v
setattr()方法接受3个参数:setattr(对象,属性,属性的值)
setattr(self,k,v)相当于self.k = v
kw.iteritems()历遍字典kw的所有key和value,分别匹配k,v
2016-03-05
def format_name(s):
return s.upper[:1]+s.lower[1:]
print map(, [format_name,['adam', 'LISA', 'barT'])
return s.upper[:1]+s.lower[1:]
print map(, [format_name,['adam', 'LISA', 'barT'])
2016-03-04
class Person(object):
def __init__(self,name,gender,birth,job):
self.name=name
self.gender=gender
self.birth=birth
self.job=job
xiaoming = Person('Xiao Ming', 'Male', '1990-1-1', job='Student')
print xiaoming.name
print xiaoming.job
def __init__(self,name,gender,birth,job):
self.name=name
self.gender=gender
self.birth=birth
self.job=job
xiaoming = Person('Xiao Ming', 'Male', '1990-1-1', job='Student')
print xiaoming.name
print xiaoming.job
2016-03-04
class Person(object):
pass
p1 = Person()
p1.name = 'Bart'
p2 = Person()
p2.name = 'Adam'
p3 = Person()
p3.name = 'Lisa'
L1 = [p1, p2, p3]
L2 = sorted(L1,lambda p1,p2:cmp(p1.name,p2.name))
print L2[0].name
print L2[1].name
print L2[2].name
pass
p1 = Person()
p1.name = 'Bart'
p2 = Person()
p2.name = 'Adam'
p3 = Person()
p3.name = 'Lisa'
L1 = [p1, p2, p3]
L2 = sorted(L1,lambda p1,p2:cmp(p1.name,p2.name))
print L2[0].name
print L2[1].name
print L2[2].name
2016-03-04
class Person(object):
pass
xiaoming = Person()
xiaohong = Person()
print xiaoming
print xiaohong
print xiaoming==xiaohong
pass
xiaoming = Person()
xiaohong = Person()
print xiaoming
print xiaohong
print xiaoming==xiaohong
2016-03-04
try:
import json
except ImportError:
import simplejson as json
print json.dumps({'python':2.7})
import json
except ImportError:
import simplejson as json
print json.dumps({'python':2.7})
2016-03-04
import math
def is_sqr(x):
if math.sqrt(x)%1==0:
return x
print filter(is_sqr, range(1, 101))
def is_sqr(x):
if math.sqrt(x)%1==0:
return x
print filter(is_sqr, range(1, 101))
2016-03-04
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-03-04