python3以上版本,reduce()函数已经不是核心函数了,函数被挪到functools库中,写程序的时候需要加上“from functools import reduce"。
2018-08-10
#这个闭包和迭代器放一起,挺好的,受教了.
#返回的是一个函数组成的可迭代对象.
#厉害了.
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
f1, f2, f3 = count()
print (f1(), f2(), f3())
#返回的是一个函数组成的可迭代对象.
#厉害了.
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
f1, f2, f3 = count()
print (f1(), f2(), f3())
2018-08-07
#闭包
import functools
def chengji(x,y):
return x*y
def calc_prod(lst):
def inner():
return functools.reduce(chengji,lst)
return inner
f = calc_prod([1, 2, 3, 4])
print f()
import functools
def chengji(x,y):
return x*y
def calc_prod(lst):
def inner():
return functools.reduce(chengji,lst)
return inner
f = calc_prod([1, 2, 3, 4])
print f()
2018-08-07
def calc_prod(lst):
def calc():
def xy(x, y):
return x*y
return reduce(xy, lst)
return calc
f = calc_prod([1, 2, 3, 4])
print f()
def calc():
def xy(x, y):
return x*y
return reduce(xy, lst)
return calc
f = calc_prod([1, 2, 3, 4])
print f()
2018-08-07
def cmp_ignore_case(s1, s2):
if str.lower(s1) < str.lower(s2):
return -1
elif str.lower(s1) > str.lower(s2):
return 1
else:
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
if str.lower(s1) < str.lower(s2):
return -1
elif str.lower(s1) > str.lower(s2):
return 1
else:
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2018-08-07
def calc_prod(lst):
def prod():
sum=1
for i,v in enumerate(lst):
sum*=v
return sum
return prod
f = calc_prod([1, 2, 3, 4])
print f()
def prod():
sum=1
for i,v in enumerate(lst):
sum*=v
return sum
return prod
f = calc_prod([1, 2, 3, 4])
print f()
2018-08-06
import math
def is_sqr(x):
y=math.sqrt(x)
return y==int(y)
print filter(is_sqr, range(1, 101))
def is_sqr(x):
y=math.sqrt(x)
return y==int(y)
print filter(is_sqr, range(1, 101))
2018-08-05
class Person(object):
def __init__(self, name, score):
self.name = name
self.__score = score
def get_grade(self):
if self.__score >80:
return 'A-优秀'
elif self.__score >=60:
return 'B-及格'
else:
return 'C-不及格'
def __init__(self, name, score):
self.name = name
self.__score = score
def get_grade(self):
if self.__score >80:
return 'A-优秀'
elif self.__score >=60:
return 'B-及格'
else:
return 'C-不及格'
2018-08-05
class Person(object):
def __init__(self, name, score):
self.name = name
self.__score = score
p = Person('Bob', 59)
try:
print p.name
print p.__score
except AttributeError:
print p.name
print 'attributeerror'
这个问题很简单,捕获异常信息,并且输出异常名称,可以通过编译器。
def __init__(self, name, score):
self.name = name
self.__score = score
p = Person('Bob', 59)
try:
print p.name
print p.__score
except AttributeError:
print p.name
print 'attributeerror'
这个问题很简单,捕获异常信息,并且输出异常名称,可以通过编译器。
2018-08-05
class Person(object):
__count = 0
def __init__(self, name):
self.name = name
Person.__count +=1
print Person.__count
p1 = Person('Bob')
p2 = Person('Alice')
try:
print Person.__count
except:
print 'attributeerror'
__count = 0
def __init__(self, name):
self.name = name
Person.__count +=1
print Person.__count
p1 = Person('Bob')
p2 = Person('Alice')
try:
print Person.__count
except:
print 'attributeerror'
2018-08-05
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
2018-08-05
def format_name(s):
return s.title()
print map(format_name, ['adam', 'LISA', 'barT'])
return s.title()
print map(format_name, ['adam', 'LISA', 'barT'])
2018-08-04