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
也可以这么写
class People(object):
def __init__(self,name,gender,birth,*args):
self.name = name
self.gender = gender
self.birth = birth
self.args = args
xiaoming = Person('Xiao Ming', 'Male', '1990-1-1', job='Student')
print xiaoming.name
print xiaoming.job
class People(object):
def __init__(self,name,gender,birth,*args):
self.name = name
self.gender = gender
self.birth = birth
self.args = args
xiaoming = Person('Xiao Ming', 'Male', '1990-1-1', job='Student')
print xiaoming.name
print xiaoming.job
2015-08-17
按这个代码输出s就变得奇葩了。。。。只要@performance()里填的不是ms而是其他字符,显示出的数值都是s为单位的值,然后再接这个字符。。。。。。。。
2015-08-17
class Fib(object):
lst = [0,1]
def __init__(self, num):
self.num = num
for x in range(1,num-1):
Fib.lst.append(sum(Fib.lst[-2:]))
def __len__(self):
return self.num
def __str__(self):
return '%s' % self.lst
f = Fib(10)
print f
print len(f)
lst = [0,1]
def __init__(self, num):
self.num = num
for x in range(1,num-1):
Fib.lst.append(sum(Fib.lst[-2:]))
def __len__(self):
return self.num
def __str__(self):
return '%s' % self.lst
f = Fib(10)
print f
print len(f)
2015-08-17