class Teacher(Person):
def __init__(self, name, gender, course):
super(Teacher,self).__init__(name,gender)
self.course=course
def __init__(self, name, gender, course):
super(Teacher,self).__init__(name,gender)
self.course=course
2015-03-16
class Person(object):
__count = 0
def __init__(self,name):
self.name=name
Person.__count += 1
@classmethod
def how_many(cls):
return cls.__count
__count = 0
def __init__(self,name):
self.name=name
Person.__count += 1
@classmethod
def how_many(cls):
return cls.__count
2015-03-16
Pipy的发音应该是"pie-pie"(根据《Head First Python》中文版)
2015-03-16
def calc_prod(lst):
def prod():
return reduce(lambda x, y: x*y, lst)
return prod
f = calc_prod([1, 2, 3, 4])
print f()
def prod():
return reduce(lambda x, y: x*y, lst)
return prod
f = calc_prod([1, 2, 3, 4])
print f()
2015-03-15
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
加个小括号就好了哇
2015-03-15
def calc_prod(lst):
def lazy_prod():
a=1
for x in lst:
a=a*x
return a
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
def lazy_prod():
a=1
for x in lst:
a=a*x
return a
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
2015-03-15
这个要用到参数名,只好去查文档了。可以上python.org去下载Documents。
sorted_ignore_case = functools.partial(sorted,key=str.lower)
sorted_ignore_case = functools.partial(sorted,key=str.lower)
2015-03-15
关于这个装饰者模式,不懂的话最好反复学习直到弄懂。。。再看一遍终于明白了orz
def performance(unit):
def new_fn(f):
def inner_fn(*args,**kw):
print 'call factorial(),'
return f(*args,**kw)
return inner_fn
return new_fn
def performance(unit):
def new_fn(f):
def inner_fn(*args,**kw):
print 'call factorial(),'
return f(*args,**kw)
return inner_fn
return new_fn
2015-03-14
晕死。。一定要加上'call factorial() in'这句话。。
def performance(f):
def fn(*args, **kw):
print 'call factorial() in',time.time()
return f(*args, **kw)
return fn
def performance(f):
def fn(*args, **kw):
print 'call factorial() in',time.time()
return f(*args, **kw)
return fn
2015-03-14
print filter(lambda s: s and len(s.strip()) > 0, ['test', None, '', 'str', ' ', 'END'])
2015-03-14
def calc_prod(lst):
def fn(x,y):
return x*y
def cal_result():
return reduce(fn,lst)
return cal_result
def fn(x,y):
return x*y
def cal_result():
return reduce(fn,lst)
return cal_result
2015-03-13