class Person(object):
__count = 0
def __init__(self, name):
self.name=name
Person.__count=Person.__count+1
print Person.__count
p1 = Person('Bob')
p2 = Person('Alice')
try:
print p1.__count+p2.__count
except AttributeError:
print "attributeerror"
__count = 0
def __init__(self, name):
self.name=name
Person.__count=Person.__count+1
print Person.__count
p1 = Person('Bob')
p2 = Person('Alice')
try:
print p1.__count+p2.__count
except AttributeError:
print "attributeerror"
2018-01-23
print 'call %s() in %fs' % (f.__name__, (t2 - t1))这句不懂啊
2018-01-23
def format_name(s):
s = s.replace(' ','')
return s[0].upper() + s[1:].lower()
print map(format_name, ['adam', 'LISA', 'barT',' sadOIdaD as'])
s = s.replace(' ','')
return s[0].upper() + s[1:].lower()
print map(format_name, ['adam', 'LISA', 'barT',' sadOIdaD as'])
2018-01-23
import functools
sorted_ignore_case = functools.partial(sorted,key=str.lower)
print sorted_ignore_case(['bob', 'about', 'Zoo', 'Credit'])
sorted_ignore_case = functools.partial(sorted,key=str.lower)
print sorted_ignore_case(['bob', 'about', 'Zoo', 'Credit'])
2018-01-23
最新回答 / 慕圣2386694
def count(): fs = [] for i in range(1, 4): def f(j): def g(): return j*j return g r = f(i) fs.append(r) return fsf1, f2, f3 = count()print f1(), f2(), f3()这样改后
2018-01-23
import time
def performance(unit):
def mid(f):
def ins(*args, **kw):
print 'call%s()... %s%s' % (f.__name__,time.time(),unit)
return f(*args, **kw)
return ins
return mid
@performance('ms')
后面是题目里默认的那堆代码
def performance(unit):
def mid(f):
def ins(*args, **kw):
print 'call%s()... %s%s' % (f.__name__,time.time(),unit)
return f(*args, **kw)
return ins
return mid
@performance('ms')
后面是题目里默认的那堆代码
2018-01-23
def calc_prod(lst):
def lazy_calc():
return functools.reduce(lambda x,y : x*y,lst)
return lazy_calc
a = [1,2,3,4]
print(calc_prod(a))
print(calc_prod(a)())
<function calc_prod.<locals>.lazy_calc at 0x0000000004E1ABF8>
24
def lazy_calc():
return functools.reduce(lambda x,y : x*y,lst)
return lazy_calc
a = [1,2,3,4]
print(calc_prod(a))
print(calc_prod(a)())
<function calc_prod.<locals>.lazy_calc at 0x0000000004E1ABF8>
24
2018-01-23
我觉得是这样理解
f = cals_sum([1,2,3,4]) => 执行的结果是return lazy_sum这个函数,而此时lazy_sum并没有执行,所以返回的不是内部函数执行的结果
f() = cals_sum(lazy_sum([1,2,3,4])) 就要执行内部函数了,内部函数执行完了,把值返回给lazy_sum,再返回给外层函数
f = cals_sum([1,2,3,4]) => 执行的结果是return lazy_sum这个函数,而此时lazy_sum并没有执行,所以返回的不是内部函数执行的结果
f() = cals_sum(lazy_sum([1,2,3,4])) 就要执行内部函数了,内部函数执行完了,把值返回给lazy_sum,再返回给外层函数
2018-01-23