class person(object):
size = 20
xiaoming = person()
xiaohong = person()
print xiaoming.size
print xiaohong.size
print xiaoming==xiaohong
size = 20
xiaoming = person()
xiaohong = person()
print xiaoming.size
print xiaohong.size
print xiaoming==xiaohong
2018-08-03
def performance(f):
def fn(n):
start=time.time()
f(n)
end=time.time()-start
print 'call %s() in %fs' % (f.__name__, end)
return f(n)
return fn
@performance
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1))
print factorial(10)
def fn(n):
start=time.time()
f(n)
end=time.time()-start
print 'call %s() in %fs' % (f.__name__, end)
return f(n)
return fn
@performance
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1))
print factorial(10)
2018-08-02
def calc_prod(lst):
def calc_prod():
sum = 1;
for x in lst:
sum = sum*x
return sum
return calc_prod
f = calc_prod([1, 2, 3, 4])
print f()
def calc_prod():
sum = 1;
for x in lst:
sum = sum*x
return sum
return calc_prod
f = calc_prod([1, 2, 3, 4])
print f()
2018-08-01
如果这里代码始终不通过,可以参考一下class Person(object):
def __init__(self, name, score):
self.name = name
self._score = score
p = Person('Bob', 59)
print p.name
print p._score
print 'attributeerror'
def __init__(self, name, score):
self.name = name
self._score = score
p = Person('Bob', 59)
print p.name
print p._score
print 'attributeerror'
2018-07-31
time.time( ) 返回当前时间的时间戳(1970纪元后经过的浮点秒数)
time.clock( ) 以浮点数计算的秒数返回当前的CPU时间。用来衡量不同程序的耗时。
%S 秒(00-59)
time.clock( ) 以浮点数计算的秒数返回当前的CPU时间。用来衡量不同程序的耗时。
%S 秒(00-59)
2018-07-31
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)
2018-07-31
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])
2018-07-31
def format_name(s):
return s[:1].upper()+s[1:].lower()
print map(format_name, ['adam', 'LISA', 'barT'])
return s[:1].upper()+s[1:].lower()
print map(format_name, ['adam', 'LISA', 'barT'])
2018-07-31
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)
2018-07-30