def count():
fs = []
for i in range(1, 4):
def f():
int y
y=i*i
return y
fs.append(f)
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
fs = []
for i in range(1, 4):
def f():
int y
y=i*i
return y
fs.append(f)
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
2016-03-07
def cmp_ignore_case(s1, s2):
if s1.lower()>s2.lower():
return 1
if s1.lower()<s2.lower():
return -1
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
if s1.lower()>s2.lower():
return 1
if s1.lower()<s2.lower():
return -1
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2016-03-06
import math
def is_sqr(x):
if math.sqrt(x)%1==0:
return x
print filter(is_sqr, range(1, 101))
def is_sqr(x):
if math.sqrt(x)%1==0:
return x
print filter(is_sqr, range(1, 101))
2016-03-06
def cmp_ignore_case(s1, s2):
if s1.lower>s2.lower:
return 1
if s1.lower<s2.lower:
return -1
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
if s1.lower>s2.lower:
return 1
if s1.lower<s2.lower:
return -1
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2016-03-06
def prod(x, y):
return x*y
print reduce(prod, [2, 4, 5, 7, 12])
return x*y
print reduce(prod, [2, 4, 5, 7, 12])
2016-03-06
s.append(f),只是将每一个f()的引用保存进了list,并没有进行对于i的计算,所以导致最后在运行了f(1)之后,i已经变为了3,所以会一样结果都为9.只要将这里改为fs.append(f())即可,这样就在这一步的时候已经进行了i*i的运算,将结果保存了,通过这一题可以注意到在python这门语言之中,f与f()的大区别。
2016-03-06
def calc_prod(lst):
def f(x,y):
return x*y
def lazy_prod():
return reduce(f,lst)
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
def f(x,y):
return x*y
def lazy_prod():
return reduce(f,lst)
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
2016-03-06
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-03-06
import json
class Students(object):
def read(self):
return r'["Tim","Bob","Alice"]'
s = Students()
print json.load(s)
class Students(object):
def read(self):
return r'["Tim","Bob","Alice"]'
s = Students()
print json.load(s)
2016-03-06
def calc_prod(lst):
def cheng(x,y):
return x*y
def a(list):
return map(cheng,lst)
return a
f = calc_prod([1, 2, 3, 4])
print f()
def cheng(x,y):
return x*y
def a(list):
return map(cheng,lst)
return a
f = calc_prod([1, 2, 3, 4])
print f()
2016-03-06
class Person(object):
__count = 0
@classmethod
def how_many(cls):
return cls.__count
def __init__(self,name):
self.name=name
Person.__count=Person.__count+1
print Person.how_many()
p1 = Person('Bob')
print Person.how_many()
__count = 0
@classmethod
def how_many(cls):
return cls.__count
def __init__(self,name):
self.name=name
Person.__count=Person.__count+1
print Person.how_many()
p1 = Person('Bob')
print Person.how_many()
2016-03-06
直接把 lambda 函数赋值给 self.get_grade 和绑定方法有所不同,函数调用不需要传入 self,但是方法调用需要传入 self。
2016-03-06
class Person(object):
count=0
def __init__(self,name):
self.name=name
Person.count=Person.count+1
p1 = Person('Bob')
print Person.count
p2 = Person('Alice')
print Person.count
p3 = Person('Tim')
print Person.count
count=0
def __init__(self,name):
self.name=name
Person.count=Person.count+1
p1 = Person('Bob')
print Person.count
p2 = Person('Alice')
print Person.count
p3 = Person('Tim')
print Person.count
2016-03-06