import json
class Students(object):
def __init__(self):
self.names= r'["Tim", "Bob", "Alice"]'
def read(self):
return self.names
s = Students()
print json.load(s)
class Students(object):
def __init__(self):
self.names= r'["Tim", "Bob", "Alice"]'
def read(self):
return self.names
s = Students()
print json.load(s)
2016-05-05
import time
def performance(unit):
def decorator(f):
def fn(*args,**kw):
print 'call %s() in %s'%(f.__name__,unit)
return f(*args,**kw)
return fn
return decorator
def performance(unit):
def decorator(f):
def fn(*args,**kw):
print 'call %s() in %s'%(f.__name__,unit)
return f(*args,**kw)
return fn
return decorator
2016-05-05
def performance(f):
def fn(*args, **kw):
start = time.time() * 1000
result = f(*args, **kw);
end = time.time() * 1000
print 'call ' + f.__name__ + '() in ' + str(end - start) + 'ms'
return result
return fn;
def fn(*args, **kw):
start = time.time() * 1000
result = f(*args, **kw);
end = time.time() * 1000
print 'call ' + f.__name__ + '() in ' + str(end - start) + 'ms'
return result
return fn;
2016-05-05
def calc_prod(lst):
def calc_delay():
s=1
for x in lst:
s=s*x
return s
return calc_delay
f = calc_prod([1, 2, 3, 4])
print f()
def calc_delay():
s=1
for x in lst:
s=s*x
return s
return calc_delay
f = calc_prod([1, 2, 3, 4])
print f()
2016-05-04
def cmp_ignore_case(s1, s2):
if s1.upper()>s2.upper():
return 1
elif s1.upper()<s2.upper():
return -1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
if s1.upper()>s2.upper():
return 1
elif s1.upper()<s2.upper():
return -1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2016-05-04
self.p * r.q + self.q * r.p, self.q * r.q
就是p/q + r.p/r.q = ( p * r.q + q * r.p ) /q * r.q
self省略看得清爽些
就是p/q + r.p/r.q = ( p * r.q + q * r.p ) /q * r.q
self省略看得清爽些
2016-05-04
def calc_prod(lst):
def test():
mul=1;
for i in lst:
mul*=i
return mul
return test
f = calc_prod([1, 2, 3, 4])
print f()
def test():
mul=1;
for i in lst:
mul*=i
return mul
return test
f = calc_prod([1, 2, 3, 4])
print f()
2016-05-04
def cmp_ignore_case(s1, s2):
temp1=s1.lower();
temp2=s2.lower();
if temp1<temp2:
return -1;
if temp1>temp2:
return 1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'],cmp_ignore_case)
temp1=s1.lower();
temp2=s2.lower();
if temp1<temp2:
return -1;
if temp1>temp2:
return 1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'],cmp_ignore_case)
2016-05-04
python3,p1.get_grade = types.MethodType(fn_get_grade, p1, Person)改为p1.get_grade = types.MethodType(fn_get_grade, p1)
2016-05-04
def count():
fs = []
for i in range(1, 4):
def return_data(j):
def f():
return j*j
return f
fs.append(return_data(i))
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
fs = []
for i in range(1, 4):
def return_data(j):
def f():
return j*j
return f
fs.append(return_data(i))
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
2016-05-04
def calc_prod(lst):
def return_prod():
def lazy_calc_prod(x,y):
return x*y
return reduce(lazy_calc_prod,lst)
return return_prod
f = calc_prod([1, 2, 3, 4])
print f()
def return_prod():
def lazy_calc_prod(x,y):
return x*y
return reduce(lazy_calc_prod,lst)
return return_prod
f = calc_prod([1, 2, 3, 4])
print f()
2016-05-04
def cmp_ignore_case(s1, s2):
return cmp(s1.lower(),s2.lower())
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
return cmp(s1.lower(),s2.lower())
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2016-05-04