class Students(object):
def __init__(self):
pass
def read(self):
return r'["Tim","Bob","Alice"]'
def __init__(self):
pass
def read(self):
return r'["Tim","Bob","Alice"]'
2015-03-16
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
最新回答 / a412739861
因为前面还有filter()函数啊。 Python 内置的另一个有用的高阶函数,filter()函数接收一个函数 f 和一个list,这个函数 f 的作用是对每个元素进行判断,返回 True或 False,filter()根据判断结果自动过滤掉不符合条件的元素,返回由符合条件元素组成的新list。http://www.imooc.com/code/6051
2015-03-15
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