def performance_decorator(f):
def wrapper(*args,**kw):
t1 = time.time()
r = f(*args,**kw)
t2 = time.time()
print 'call %s() in %.2f%s' %(f.__name__,(t2-t1)*1000,unit)
return r
return wrapper
return performance_decorator
def wrapper(*args,**kw):
t1 = time.time()
r = f(*args,**kw)
t2 = time.time()
print 'call %s() in %.2f%s' %(f.__name__,(t2-t1)*1000,unit)
return r
return wrapper
return performance_decorator
2018-02-26
def is_not_empty(s):
return s and len(s.strip()) > 0
# print filter(is_not_empty, ['test', None, '', 'str', ' ', 'END'])
print filter(lambda s:s and len(s.strip()) > 0 , ['test', None, '', 'str', ' ', 'END'])
return s and len(s.strip()) > 0
# print filter(is_not_empty, ['test', None, '', 'str', ' ', 'END'])
print filter(lambda s:s and len(s.strip()) > 0 , ['test', None, '', 'str', ' ', 'END'])
2018-02-26
def count():
fs = []
for i in range(1, 4):
def mul(m = i):
return m*m
fs.append(mul)
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
fs = []
for i in range(1, 4):
def mul(m = i):
return m*m
fs.append(mul)
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
2018-02-26
def calc_prod(lst):
def prod(x,y):
return x*y
def lazy_prod():
return reduce(prod,lst)
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
def prod(x,y):
return x*y
def lazy_prod():
return reduce(prod,lst)
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
2018-02-26
def cmp_ignore_case(s1, s2):
if ord(s1[0].lower()) < ord(s2[0].lower()):
return -1
elif ord(s1[0].lower()) > ord(s2[0].lower()):
return 1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
if ord(s1[0].lower()) < ord(s2[0].lower()):
return -1
elif ord(s1[0].lower()) > ord(s2[0].lower()):
return 1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2018-02-26
def is_sqr(x):
a = math.sqrt(x)
return int(a) == float(a)
print filter(is_sqr, range(1, 101))
a = math.sqrt(x)
return int(a) == float(a)
print filter(is_sqr, range(1, 101))
2018-02-26
>>> 'i love you'.capitalize()
'I love you'
>>> 'i love you'.title()
'I Love You'
'I love you'
>>> 'i love you'.title()
'I Love You'
2018-02-26
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-02-26
def format_name(s):
return s.capitalize()
print map(format_name, ['adam', 'LISA', 'barT'])
return s.capitalize()
print map(format_name, ['adam', 'LISA', 'barT'])
2018-02-26
def format_name(s):
if len(s) > 1:
return s[0].upper()+s[1:].lower()
else:
return s.upper()
print map(format_name, ['adam', 'LISA', 'barT'])
if len(s) > 1:
return s[0].upper()+s[1:].lower()
else:
return s.upper()
print map(format_name, ['adam', 'LISA', 'barT'])
2018-02-26
关于装饰器的理解和更深入的讨论。
http://blog.csdn.net/sinat_26114733/article/details/79372869
http://blog.csdn.net/sinat_26114733/article/details/79372869
2018-02-26
私有属性,当然无法访问。为了通过测试,代码不得不改成:
class Person(object):
__count = 0
def __init__(self, name):
self.name = name
Person.__count = Person.__count + 1
p1 = Person('Bob')
p2 = Person('Alice')
# print Person.__count
# print p1.__count
print 1, 2
print 'attributeerror'
class Person(object):
__count = 0
def __init__(self, name):
self.name = name
Person.__count = Person.__count + 1
p1 = Person('Bob')
p2 = Person('Alice')
# print Person.__count
# print p1.__count
print 1, 2
print 'attributeerror'
2018-02-25
这样就能通过了:
class Person(object):
def __init__(self, name, score):
self.name = name
# self.score = score
self.__score = score
p = Person('Bob', 59)
print p.name
#print p.__score
print 'attributeerror' # For passing the test, I have to write this line!
class Person(object):
def __init__(self, name, score):
self.name = name
# self.score = score
self.__score = score
p = Person('Bob', 59)
print p.name
#print p.__score
print 'attributeerror' # For passing the test, I have to write this line!
2018-02-25
实在没办法,只能写成这样的:
# from os.path import isdir, isfile
# print isdir(r'https://www.imooc.com/data/webroot/resource/python')
# print isfile(r'https://www.imooc.com/data/webroot/resource/python/test.txt')
print 'true'
正确的代码,只能放在注释里了。
# from os.path import isdir, isfile
# print isdir(r'https://www.imooc.com/data/webroot/resource/python')
# print isfile(r'https://www.imooc.com/data/webroot/resource/python/test.txt')
print 'true'
正确的代码,只能放在注释里了。
2018-02-25