from functools import reduce
def calc_prod(lst):
def lazy_prod():
return reduce(lambda x, y: x*y, lst)
return lazy_prod
f = cacl_prod([1, 2, 3, 4])
print(f())
def calc_prod(lst):
def lazy_prod():
return reduce(lambda x, y: x*y, lst)
return lazy_prod
f = cacl_prod([1, 2, 3, 4])
print(f())
2016-01-17
python3用法:
a = ['bob', 'about', 'Zoo', 'Credit']
print(sorted(a, key=str.lower))
a = ['bob', 'about', 'Zoo', 'Credit']
print(sorted(a, key=str.lower))
2016-01-17
import math
def is_sqr(x):
return str(math.sqrt(x)).split('.')[-1] == '0'
print filter(is_sqr, range(1, 101))
def is_sqr(x):
return str(math.sqrt(x)).split('.')[-1] == '0'
print filter(is_sqr, range(1, 101))
2016-01-17
class Person(object):
def __init__(self, name, score):
self.name = name
self.__score = score
p = Person('Bob', 59)
print p.name
try:
print p.__score
except AttributeError:
print ('attributeError')
def __init__(self, name, score):
self.name = name
self.__score = score
p = Person('Bob', 59)
print p.name
try:
print p.__score
except AttributeError:
print ('attributeError')
2016-01-16
def __str__(self):
x = min(self.p, self.q)
while self.p % x != 0 or self.q % x != 0:
x -= 1
if x == 1:
break
return "%s/%s" % (self.p/x, self.q/x)
x = min(self.p, self.q)
while self.p % x != 0 or self.q % x != 0:
x -= 1
if x == 1:
break
return "%s/%s" % (self.p/x, self.q/x)
2016-01-16
def calc_prod(lst):
def mul():
if not isinstance(lst, list):
# raise Exception()
print 'arg is not a list'
return
else:
return reduce(lamda: x,y: x*y, lst)
return mul
f = calc_prod([1, 2, 3, 4])
print f()
这段代码哪里有问题,请问?
def mul():
if not isinstance(lst, list):
# raise Exception()
print 'arg is not a list'
return
else:
return reduce(lamda: x,y: x*y, lst)
return mul
f = calc_prod([1, 2, 3, 4])
print f()
这段代码哪里有问题,请问?
2016-01-16
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-01-16
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:
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:
print "attributeerror"
2016-01-15
def performance(unit):
def wrapper(f):
def invoke(*args, **kw):
t = time.ctime()
print "call",f.__name__+"() in",t,unit
return f(*args, **kw)
return invoke
return wrapper
def wrapper(f):
def invoke(*args, **kw):
t = time.ctime()
print "call",f.__name__+"() in",t,unit
return f(*args, **kw)
return invoke
return wrapper
2016-01-15
import math
def is_sqr(x):
return math.sqrt(x)%1 == 0
print filter(is_sqr, range(1, 101))
def is_sqr(x):
return math.sqrt(x)%1 == 0
print filter(is_sqr, range(1, 101))
2016-01-15
def calc_prod(lst):
def prod(l=lst):
return reduce(lambda x, y: x * y, l)
return prod
f = calc_prod([1, 2, 3, 4])
print f()
def prod(l=lst):
return reduce(lambda x, y: x * y, l)
return prod
f = calc_prod([1, 2, 3, 4])
print f()
2016-01-15
print sorted(['bob', 'about', 'Zoo', 'Credit'], key=str.lower)
2016-01-15