p1.get_grade()只不过是调用lambda:'A'得到返回值A而已 lambda: "A"相当于 def f(): return "A"
2016-09-20
今天算是见识了python缩进的扯淡规则,只能重新写一遍
def performance(unit):
def fn_performance(f):
def wrapper(*args, **kw):
t1 = time.time()
r = f(*args, **kw)
t = 0.0
if unit == 's':
t = time.time() - t1
else:
t = (time.time() - t1) * 1000
print "call %s in %s %s" % (f.__name__, t, unit)
def performance(unit):
def fn_performance(f):
def wrapper(*args, **kw):
t1 = time.time()
r = f(*args, **kw)
t = 0.0
if unit == 's':
t = time.time() - t1
else:
t = (time.time() - t1) * 1000
print "call %s in %s %s" % (f.__name__, t, unit)
2016-09-19
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 Person.__count
except:
print 'Attribute error'
__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 Person.__count
except:
print 'Attribute error'
2016-09-19
L2 = sorted(L1,lambda p1,p2: cmp(p1.name.lower(),p2.name.lower()))
2016-09-19
def calc_prod(lst):
def new_fun(lst):
result = 1
for i in lst:
result = result * i
return result
return new_fun(lst)
f = calc_prod([1, 2, 3, 4])
print (f)
def new_fun(lst):
result = 1
for i in lst:
result = result * i
return result
return new_fun(lst)
f = calc_prod([1, 2, 3, 4])
print (f)
2016-09-18
def count():
fs = []
for i in range(1, 4):
def f():
return i*i
fs.append(f())
return fs
f1, f2, f3 = count()
print f1, f2, f3
fs = []
for i in range(1, 4):
def f():
return i*i
fs.append(f())
return fs
f1, f2, f3 = count()
print f1, f2, f3
2016-09-18