def decorator(text):def function(func):
@functools.wraps(func)
def wrapper(*args,**kw):
t1 = time.time()
print 'factorial values : %d' % func(*args,**kw)
t2 = time.time()
T= (t2-t1)*1000 if text == 'ms' else t2-t1
print 'call %s use time: %f%s' % (func.__name__,T,text)
return wrapper
return function
@functools.wraps(func)
def wrapper(*args,**kw):
t1 = time.time()
print 'factorial values : %d' % func(*args,**kw)
t2 = time.time()
T= (t2-t1)*1000 if text == 'ms' else t2-t1
print 'call %s use time: %f%s' % (func.__name__,T,text)
return wrapper
return function
2016-01-15
import time,functools
def performance(func):
@functools.wraps(func)
def wrapper(*args,**kw):
t1 = time.time()
print 'factorial: %s' % func(*args,**kw)
t2 = time.time()
print 'use time: %fs' % (t2-t1)
return
return wrapper
def performance(func):
@functools.wraps(func)
def wrapper(*args,**kw):
t1 = time.time()
print 'factorial: %s' % func(*args,**kw)
t2 = time.time()
print 'use time: %fs' % (t2-t1)
return
return wrapper
2016-01-15
import time
def performance(f):
def new_f(n):
print "call",f.__name__+"() in",time.ctime()
return f(n)
return new_f
@performance
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1))
print factorial(10)
def performance(f):
def new_f(n):
print "call",f.__name__+"() in",time.ctime()
return f(n)
return new_f
@performance
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1))
print factorial(10)
2016-01-15
def cmp_ignore_case(s1, s2):
s1=s1.lower()
s2=s2.lower()
if s1>s2:
return 1
elif s1==s2:
return 0
else:
return -1
print sorted(['bob', 'about', 'Zoo', 'Credit'],cmp_ignore_case)
s1=s1.lower()
s2=s2.lower()
if s1>s2:
return 1
elif s1==s2:
return 0
else:
return -1
print sorted(['bob', 'about', 'Zoo', 'Credit'],cmp_ignore_case)
2016-01-14
import math
def is_sqr(x):
if math.sqrt(x).is_integer():
return x
print filter(is_sqr, range(1, 101))
def is_sqr(x):
if math.sqrt(x).is_integer():
return x
print filter(is_sqr, range(1, 101))
2016-01-14
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-01-14
def calc_prod(lst):
def prod():
return reduce(lambda r, x: r * x, lst)
return prod
f = calc_prod([1, 2, 3, 4])
print f()
def prod():
return reduce(lambda r, x: r * x, lst)
return prod
f = calc_prod([1, 2, 3, 4])
print f()
2016-01-14
def cmp_ignore_case(s1, s2):
return 1 if s1[:1].lower() > s2[:1].lower() else -1
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
return 1 if s1[:1].lower() > s2[:1].lower() else -1
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2016-01-14
import math
def is_sqr(x):
return True if math.sqrt(x) % 1 == 0 else False
print filter(is_sqr, range(1, 101))
def is_sqr(x):
return True if math.sqrt(x) % 1 == 0 else False
print filter(is_sqr, range(1, 101))
2016-01-14
class Person():
name=''
def __init__(self,n):
self.name=n
xiaoming = Person('xm')
xiaohong = Person('xh')
print xiaoming.name
print xiaohong.name
print xiaoming==xiaohong
name=''
def __init__(self,n):
self.name=n
xiaoming = Person('xm')
xiaohong = Person('xh')
print xiaoming.name
print xiaohong.name
print xiaoming==xiaohong
2016-01-14
def cmp_ignore_case(s1, s2):
if s1.upper() > s2.upper():
return 1
if s1.upper() < s2.upper():
return -1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
if s1.upper() > s2.upper():
return 1
if s1.upper() < s2.upper():
return -1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2016-01-14
import math
def add(x, y, f):
return f(x) + f(y)
print add(25, 9, math.sqrt)
def add(x, y, f):
return f(x) + f(y)
print add(25, 9, math.sqrt)
2016-01-13
class Person(object):
count = 0
def __init__(self, name):
self.name = name
Person.count = Person.count + 1
print person.__count
p1 = Person('Bob')
p2 = Person('Alice')
print Person.__count
在编译器中这样执行后:
1
2
AttributeError: type object 'Person' has no attribute '__count'
count = 0
def __init__(self, name):
self.name = name
Person.count = Person.count + 1
print person.__count
p1 = Person('Bob')
p2 = Person('Alice')
print Person.__count
在编译器中这样执行后:
1
2
AttributeError: type object 'Person' has no attribute '__count'
2016-01-13