def performance(f):
def fn(*args, **kw):
t1 = time.time()
r = f(*args, **kw)
t2 = time.time()
print'call %s() in %fs' % (f.__name__, (t2 - t1))
return r
return fn
def fn(*args, **kw):
t1 = time.time()
r = f(*args, **kw)
t2 = time.time()
print'call %s() in %fs' % (f.__name__, (t2 - t1))
return r
return fn
2016-03-15
class Fib(object):
def __call__(self, num):
L = [0, 1]
for i in range(num - 2):
L.append(L[i] + L[i +1])
return L
f = Fib()
print f(10)
def __call__(self, num):
L = [0, 1]
for i in range(num - 2):
L.append(L[i] + L[i +1])
return L
f = Fib()
print f(10)
2016-03-13
import math
def is_sqr(x):
return x and math.sqrt(x)%1==0
print filter(is_sqr, range(1,101))
def is_sqr(x):
return x and math.sqrt(x)%1==0
print filter(is_sqr, range(1,101))
2016-03-13
def __str__(self):
def divisor(a,b):
tmp=max(a,b)%min(a,b)
if tmp==0:
return min(a,b)
else:
return divisor(min(a,b),tmp)
divnum = divisor(self.p, self.q)
return '%s/%s' % (self.p / divnum, self.q / divnum)
def divisor(a,b):
tmp=max(a,b)%min(a,b)
if tmp==0:
return min(a,b)
else:
return divisor(min(a,b),tmp)
divnum = divisor(self.p, self.q)
return '%s/%s' % (self.p / divnum, self.q / divnum)
2016-03-13
class Fib(object):
def __init__(self, num):
fibl=[0,1]
for item in range(0, num -2):
fibl.append(fibl[item] + fibl[item + 1])
self.fibList = fibl
def __str__(self):
return str(self.fibList)
def __len__(self):
return len(self.fibList)
def __init__(self, num):
fibl=[0,1]
for item in range(0, num -2):
fibl.append(fibl[item] + fibl[item + 1])
self.fibList = fibl
def __str__(self):
return str(self.fibList)
def __len__(self):
return len(self.fibList)
2016-03-13
def format_name(s):
return s.title()
print map(format_name, ['adam', 'LISA', 'barT'])
return s.title()
print map(format_name, ['adam', 'LISA', 'barT'])
2016-03-13
class Person(object):
__count = 0
def __init__(self,name):
self.name = name
Person.__count += 1
@staticmethod
def how_many():
return Person.__count
@classmethod
def how_many2(cls):
return cls.__count
__count = 0
def __init__(self,name):
self.name = name
Person.__count += 1
@staticmethod
def how_many():
return Person.__count
@classmethod
def how_many2(cls):
return cls.__count
2016-03-13
# -*- coding: utf-8 -*-
class Person(object):
def get_grade(self):
if self.__score > 85:
return "A-优秀"
elif self.__score > 60:
return "B-及格"
else:
return "C-不及格"
class Person(object):
def get_grade(self):
if self.__score > 85:
return "A-优秀"
elif self.__score > 60:
return "B-及格"
else:
return "C-不及格"
2016-03-13
class Person(object):
__count = 0
def __init__(self, name):
self.name = name
Person.__count +=1
print Person.__count
p1 = Person('Bob')
p2 = Person('Alice')
try:
print Person.__count
except AttributeError:
print 'AttributeError'
__count = 0
def __init__(self, name):
self.name = name
Person.__count +=1
print Person.__count
p1 = Person('Bob')
p2 = Person('Alice')
try:
print Person.__count
except AttributeError:
print 'AttributeError'
2016-03-13
class Person(object):
count = 0
def __init__(self,name):
Person.count +=1
self.name = name
count = 0
def __init__(self,name):
Person.count +=1
self.name = name
2016-03-13
try:
print p.__score
except AttributeError:
print ('attributeError')
考察try...except的错误信息处理
print p.__score
except AttributeError:
print ('attributeError')
考察try...except的错误信息处理
2016-03-13
import time
def performance(f):
def fn(x):
t1 = time.time()
f(x)
t2 = time.time()
print "call factorial() in time = %f (ms)" % ( t2 - t1)
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(x):
t1 = time.time()
f(x)
t2 = time.time()
print "call factorial() in time = %f (ms)" % ( t2 - t1)
return fn
@performance
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1))
print factorial(10)
根本不用花费时间。
2016-03-13
def calc_prod(lst):
def prodxy():
return reduce(lambda x, y:x*y, lst)
return prodxy
f = calc_prod([1, 2, 3, 4])
print f()
def prodxy():
return reduce(lambda x, y:x*y, lst)
return prodxy
f = calc_prod([1, 2, 3, 4])
print f()
2016-03-13