class Person(object):
count = 0
def __init__(self,name):
self.name = name
Person.count = Person.count+1
p1 = Person('Bob')
print Person.count
p2 = Person('Alice')
print Person.count
p3 = Person('Tim')
print Person.count
count = 0
def __init__(self,name):
self.name = name
Person.count = Person.count+1
p1 = Person('Bob')
print Person.count
p2 = Person('Alice')
print Person.count
p3 = Person('Tim')
print Person.count
2018-08-05
已采纳回答 / 理Risen
应该是指,原函数的参数比如是f(x),经过包装后出来变成了f(**args,**kw),从固定参数x变成了动态参数**args,**kw,原本的形参x就丢失了。functools.wraps()方法无法复制这一部分的信息。
2018-08-05
最新回答 / 立志Java匠人
def performance(f): def f1(*args): t1 = time.time() f(*args) t2 = time.time() print 'call %s() in %fs' % (f.__name__,(t2-t1)) return f(*args) return f1我就是直接返回f(*args)的,老师的答案输出的时间除了函数调用时间还多了个运行时间
2018-08-04
def format_name(s):
return s.title()
print map(format_name, ['adam', 'LISA', 'barT'])
return s.title()
print map(format_name, ['adam', 'LISA', 'barT'])
2018-08-04
最新回答 / 925755208
不能,r = f(*args, **kwargs)相当于调用f(*args, **kwargs)这个函数运行(求值),r就是一个变量,函数运行结果赋值给r了,而你后面return f 表示返回一个函数(Python中函数名相当于变量,指向函数体)
2018-08-03
class person(object):
size = 20
xiaoming = person()
xiaohong = person()
print xiaoming.size
print xiaohong.size
print xiaoming==xiaohong
size = 20
xiaoming = person()
xiaohong = person()
print xiaoming.size
print xiaohong.size
print xiaoming==xiaohong
2018-08-03
已采纳回答 / 慕容天使
import mathdef is_sqr(x): return math.sqrt(x) % 1 == 0 #切记,这里返回的是一个布尔值,真/假print filter(is_sqr, range(1, 101))
2018-08-02
def performance(f):
def fn(n):
start=time.time()
f(n)
end=time.time()-start
print 'call %s() in %fs' % (f.__name__, end)
return f(n)
return fn
@performance
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1))
print factorial(10)
def fn(n):
start=time.time()
f(n)
end=time.time()-start
print 'call %s() in %fs' % (f.__name__, end)
return f(n)
return fn
@performance
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1))
print factorial(10)
2018-08-02