functools.partial就是帮助我们创建一个偏函数的,不需要我们自己定义int2(),可以直接使用下面的代码创建一个新的函数int2:
2015-08-21
def __cmp__(self, s):
if self.score!=s.score:
return s.score-self.score
else:
return cmp(self.name,s.name)
if self.score!=s.score:
return s.score-self.score
else:
return cmp(self.name,s.name)
2015-08-21
import json
class Students(object):
def __init__(self,list):
self.list=list
def read(self):
return self.list
L=r'["Tim","Bob","Alice"]'
s = Students(L)
print json.load(s)
class Students(object):
def __init__(self,list):
self.list=list
def read(self):
return self.list
L=r'["Tim","Bob","Alice"]'
s = Students(L)
print json.load(s)
2015-08-21
已采纳回答 / zh_414
打印调用函数名称的语句,若f函数名为int,那么语句执行的结果为call int()__name__是函数名的一个属性,f.__name__即为只打印函数名,其余的就是print的语法了
2015-08-20
def count():
fs = []
for i in range(1, 4):
def f(i=i):
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(i=i):
return i*i
fs.append(f)
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
2015-08-20
def count():
fs = []
for i in range(1, 4):
def f(j):
def g():
return j*j
return g
r = f(i)
fs.append(r)
return fs
f=count()
print f[0](),f[1](),f[2]()
fs = []
for i in range(1, 4):
def f(j):
def g():
return j*j
return g
r = f(i)
fs.append(r)
return fs
f=count()
print f[0](),f[1](),f[2]()
2015-08-19
4大内置函数
Filter
>>> def f(x): return x % 2 != 0 and x % 3 != 0
>>> filter(f, range(2, 25))
Map
>>> def cube(x): return x*x*x
>>> map(cube, range(1, 11))
Reduce
>>> def add(x,y): return x + y
>>> reduce(add, range(1, 11))
lambda
(lambda x: x * 2)(3)
Filter
>>> def f(x): return x % 2 != 0 and x % 3 != 0
>>> filter(f, range(2, 25))
Map
>>> def cube(x): return x*x*x
>>> map(cube, range(1, 11))
Reduce
>>> def add(x,y): return x + y
>>> reduce(add, range(1, 11))
lambda
(lambda x: x * 2)(3)
2015-08-19
import math
def is_sqr(x):
return math.sqrt(x)==int(math.sqrt(x))
print filter(is_sqr, range(1, 101))
def is_sqr(x):
return math.sqrt(x)==int(math.sqrt(x))
print filter(is_sqr, range(1, 101))
2015-08-19