def count():
fs = []
for i in range(1, 4):
def f(j):
def g():
return j*j
return g
fs.append(f(i))
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
fs = []
for i in range(1, 4):
def f(j):
def g():
return j*j
return g
fs.append(f(i))
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
2016-02-12
def calc_prod(lst):
def prod(x,y):
return x*y
def lazy_prod():
return reduce(prod,lst)
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
def prod(x,y):
return x*y
def lazy_prod():
return reduce(prod,lst)
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
2016-02-12
def cmp_ignore_case(s1, s2):
if s1.lower()<s2.lower():
return -1
elif s1.lower()>s2.lower():
return 1
else:
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
if s1.lower()<s2.lower():
return -1
elif s1.lower()>s2.lower():
return 1
else:
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2016-02-12
import math
def is_sqr(x):
return x and math.sqrt(x)-math.sqrt(x)//1==0
print filter(is_sqr, range(1, 101))
def is_sqr(x):
return x and math.sqrt(x)-math.sqrt(x)//1==0
print filter(is_sqr, range(1, 101))
2016-02-12
import math
def is_sqr(x):
for m in range(1,11):
if math.sqrt(x)==m:
return x
print filter(is_sqr, range(1, 101))
def is_sqr(x):
for m in range(1,11):
if math.sqrt(x)==m:
return x
print filter(is_sqr, range(1, 101))
2016-02-11
import math
def add(x, y, f):
return f(x) + f(y)
print add(25.0, 9.0, math.sqrt)
def add(x, y, f):
return f(x) + f(y)
print add(25.0, 9.0, math.sqrt)
2016-02-10
def count():
fs = []
for i in range(1, 4):
def f(m = i):
return m * m
fs.append(f)
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
方法:问题的产生是因为函数只在执行时才去获取外层参数i,若函数定义时可以获取到i,问题便可解决。而默认参数正好可以完成定义时获取i值且运行函数时无需参数输入的功能,所以在函数f()定义中改为f(m = i),函数f返回值改为m*m即可.
fs = []
for i in range(1, 4):
def f(m = i):
return m * m
fs.append(f)
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
方法:问题的产生是因为函数只在执行时才去获取外层参数i,若函数定义时可以获取到i,问题便可解决。而默认参数正好可以完成定义时获取i值且运行函数时无需参数输入的功能,所以在函数f()定义中改为f(m = i),函数f返回值改为m*m即可.
2016-02-10
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-02-10
解释器内部会将**kw拆分成对应的dict.
setattr()方法接受3个参数:setattr(对象,属性,属性的值)
setattr(self,k,v)相当于self.k = v
kw.iteritems()历遍字典kw的所有key和value,分别匹配k,v
setattr()方法接受3个参数:setattr(对象,属性,属性的值)
setattr(self,k,v)相当于self.k = v
kw.iteritems()历遍字典kw的所有key和value,分别匹配k,v
2016-02-10
class Person(object):
pass
p1 = Person()
p1.name = 'Bart'
p2 = Person()
p2.name = 'Adam'
p3 = Person()
p3.name = 'Lisa'
L1 = [p1, p2, p3]
L2 = sorted(L1, lambda x,y: cmp(x.name, y.name))
print L2[0].name
print L2[1].name
print L2[2].name
pass
p1 = Person()
p1.name = 'Bart'
p2 = Person()
p2.name = 'Adam'
p3 = Person()
p3.name = 'Lisa'
L1 = [p1, p2, p3]
L2 = sorted(L1, lambda x,y: cmp(x.name, y.name))
print L2[0].name
print L2[1].name
print L2[2].name
2016-02-09
def is_not_empty(s):
return s and len(s.strip()) > 0
print filter(lambda s: s and len(s.strip()) > 0, ['test', None, '', 'str', ' ', 'END'])
return s and len(s.strip()) > 0
print filter(lambda s: s and len(s.strip()) > 0, ['test', None, '', 'str', ' ', 'END'])
2016-02-09
def cmp_ignore_case(s1, s2):
if s1.upper()>s2.upper():
return 1
elif s1.upper()==s2.upper():
return 0
else:
return -1
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
if s1.upper()>s2.upper():
return 1
elif s1.upper()==s2.upper():
return 0
else:
return -1
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2016-02-09