def prod(x, y):
return x*y
print reduce(prod, [2, 4, 5, 7, 12])
return x*y
print reduce(prod, [2, 4, 5, 7, 12])
2016-03-06
s.append(f),只是将每一个f()的引用保存进了list,并没有进行对于i的计算,所以导致最后在运行了f(1)之后,i已经变为了3,所以会一样结果都为9.只要将这里改为fs.append(f())即可,这样就在这一步的时候已经进行了i*i的运算,将结果保存了,通过这一题可以注意到在python这门语言之中,f与f()的大区别。
2016-03-06
def calc_prod(lst):
def f(x,y):
return x*y
def lazy_prod():
return reduce(f,lst)
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
def f(x,y):
return x*y
def lazy_prod():
return reduce(f,lst)
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
2016-03-06
class Person(object):
def __init__(self, name, gender, **kw):
self.name=name
self.gender=gender
for k,v in kw.iteritems():
setattr(self,k,v)
p = Person('Bob', 'Male', age=18, course='Python')
print p.age
print p.course
def __init__(self, name, gender, **kw):
self.name=name
self.gender=gender
for k,v in kw.iteritems():
setattr(self,k,v)
p = Person('Bob', 'Male', age=18, course='Python')
print p.age
print p.course
2016-03-06
import json
class Students(object):
def read(self):
return r'["Tim","Bob","Alice"]'
s = Students()
print json.load(s)
class Students(object):
def read(self):
return r'["Tim","Bob","Alice"]'
s = Students()
print json.load(s)
2016-03-06
def calc_prod(lst):
def cheng(x,y):
return x*y
def a(list):
return map(cheng,lst)
return a
f = calc_prod([1, 2, 3, 4])
print f()
def cheng(x,y):
return x*y
def a(list):
return map(cheng,lst)
return a
f = calc_prod([1, 2, 3, 4])
print f()
2016-03-06
class Person(object):
__count = 0
@classmethod
def how_many(cls):
return cls.__count
def __init__(self,name):
self.name=name
Person.__count=Person.__count+1
print Person.how_many()
p1 = Person('Bob')
print Person.how_many()
__count = 0
@classmethod
def how_many(cls):
return cls.__count
def __init__(self,name):
self.name=name
Person.__count=Person.__count+1
print Person.how_many()
p1 = Person('Bob')
print Person.how_many()
2016-03-06
直接把 lambda 函数赋值给 self.get_grade 和绑定方法有所不同,函数调用不需要传入 self,但是方法调用需要传入 self。
2016-03-06
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
2016-03-06
class Person(object):
def __init__(self, name, score):
self.name=name
self.__score=score
p = Person('Bob', 59)
print p.name
print p.__score
def __init__(self, name, score):
self.name=name
self.__score=score
p = Person('Bob', 59)
print p.name
print p.__score
2016-03-06
def performance(unit):
def perf_decorator(f):
@functools.wraps(f)
def wrapper(*args, **kw):
print 'call',f._name_+'() in',time().unit
return wrapper
return perf_decorator
def perf_decorator(f):
@functools.wraps(f)
def wrapper(*args, **kw):
print 'call',f._name_+'() in',time().unit
return wrapper
return perf_decorator
2016-03-06
这是改正之后的,上一个答案里面 in 前面要加一个空格才能通过。至于为什么time().unit能通过我也不知道,自己乱试的,也许内置有这个时间格式函数 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
2016-03-06
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