print (list(filter(lambda x:x and len(x.strip())>0, ['test', None, '', 'str', ' ', 'END'])))
3.4版本的和老版本还是差别挺大的
3.4版本的和老版本还是差别挺大的
2016-02-17
class Person(object):
__count = 0
def __init__(self, name):
Person.__count = Person.__count + 1
self.name = name
print Person.__count
p1 = Person('Bob')
p2 = Person('Alice')
try:
print Person.__count
except AttributeError:
print 'AttributeError'
__count = 0
def __init__(self, name):
Person.__count = Person.__count + 1
self.name = name
print Person.__count
p1 = Person('Bob')
p2 = Person('Alice')
try:
print Person.__count
except AttributeError:
print 'AttributeError'
2016-02-16
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-02-16
def format_name(s):
return s.capitalize()
print map(format_name, ['adam', 'LISA', 'barT'])
return s.capitalize()
print map(format_name, ['adam', 'LISA', 'barT'])
2016-02-16
def performance(f):
def fn(*args, **kw):
t = time.strftime('%Y-%m-%d %H:%M:%S')
print 'call' + f.__name__ + '()in...' + t
return f(*args, **kw)
return fn
@performance
def factorial(n):
return reduce(lambda x, y: x * y, range(1, n + 1))
print factorial(10)
def fn(*args, **kw):
t = time.strftime('%Y-%m-%d %H:%M:%S')
print 'call' + f.__name__ + '()in...' + t
return f(*args, **kw)
return fn
@performance
def factorial(n):
return reduce(lambda x, y: x * y, range(1, n + 1))
print factorial(10)
2016-02-16
def calc_prod(lst):
def prod():
s=1
for i in lst:
s=s*i
return s
return prod
f = calc_prod([1, 2, 3, 4])
print f()
def prod():
s=1
for i in lst:
s=s*i
return s
return prod
f = calc_prod([1, 2, 3, 4])
print f()
2016-02-16
def cmp_ignore_case(s1, s2):
if s1[0].upper()<s2[0].upper():
return -1
elif s1[0].upper()>s2[0].upper():
return 1
else:
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
if s1[0].upper()<s2[0].upper():
return -1
elif s1[0].upper()>s2[0].upper():
return 1
else:
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2016-02-15
import math
def is_sqr(x):
sq = math.sqrt(x)
if sq % 1 == 0:
return x
print filter(is_sqr, range(1, 101))
def is_sqr(x):
sq = math.sqrt(x)
if sq % 1 == 0:
return x
print filter(is_sqr, range(1, 101))
2016-02-15
import time
def performance(unit):
def performance_decorator(f):
def wrapper(*args,**kw):
print 'call'+f.__name__+'()...'
return f(*args,**kw)
return wrapper
return performance_decorator
def performance(unit):
def performance_decorator(f):
def wrapper(*args,**kw):
print 'call'+f.__name__+'()...'
return f(*args,**kw)
return wrapper
return performance_decorator
2016-02-12
import time
def performance(f):
def fn(*args,**kw):
print 'call'+f.__name__+'()in...'
return f(*args,**kw)
return fn
@performance
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1))
print factorial(10)
def performance(f):
def fn(*args,**kw):
print 'call'+f.__name__+'()in...'
return f(*args,**kw)
return fn
@performance
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1))
print factorial(10)
2016-02-12
print filter(lambda s: s and len(s.strip())>0 , ['test', None, '', 'str', ' ', 'END'])
2016-02-12