print 'call %s () in %fs' % (f.__name__, (t2 - t1))
%s()。。。。s和括号中间需要加个空格
%s()。。。。s和括号中间需要加个空格
2018-10-08
分享代码
from os.path import isdir,isfile
print 'True'
print 'True'
from os.path import isdir,isfile
print 'True'
print 'True'
2018-10-07
分享代码
def cmp_ignore_case(s1, s2):
if s1.lower() < s2.lower():
return -1
if s1.lower() == s2.lower():
return 0
else:
return 1
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
def cmp_ignore_case(s1, s2):
if s1.lower() < s2.lower():
return -1
if s1.lower() == s2.lower():
return 0
else:
return 1
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2018-10-07
分享答案
import math
def is_sqr(x):
return math.sqrt(x)%1==0
print filter(is_sqr, range(1, 101))
import math
def is_sqr(x):
return math.sqrt(x)%1==0
print filter(is_sqr, range(1, 101))
2018-10-07
class Fib(object):
def __call__(self,v):
pre,curr,L= 0,1,[]
for i in range(v):
L.append(curr)
pre,curr = curr,pre+curr
return L
f = Fib()
print f(10)
def __call__(self,v):
pre,curr,L= 0,1,[]
for i in range(v):
L.append(curr)
pre,curr = curr,pre+curr
return L
f = Fib()
print f(10)
2018-10-06
class Person(object):
__count = 0
@classmethod
def how_many(cls):
return cls.__count
def __init__(self,name):
self.name = name
Person.__count += 1
print Person.how_many()
p1 = Person('Bob')
print Person.how_many()
__count = 0
@classmethod
def how_many(cls):
return cls.__count
def __init__(self,name):
self.name = name
Person.__count += 1
print Person.how_many()
p1 = Person('Bob')
print Person.how_many()
2018-10-05
class Person(object):
pass
p1 = Person()
p1.name = 'Bart'
p2 = Person()
p2.name = 'Adam'
p3 = Person()
p3.name = 'Lisa'
L1 = [p1, p2, p3]
L2 = sorted(L1,key=lambda p: p.name)
print L2[0].name
print L2[1].name
print L2[2].name
pass
p1 = Person()
p1.name = 'Bart'
p2 = Person()
p2.name = 'Adam'
p3 = Person()
p3.name = 'Lisa'
L1 = [p1, p2, p3]
L2 = sorted(L1,key=lambda p: p.name)
print L2[0].name
print L2[1].name
print L2[2].name
2018-10-05
class Person:
pass
xiaoming = Person()
xiaohong = Person()
print xiaoming
print xiaohong
print xiaoming == xiaohong
pass
xiaoming = Person()
xiaohong = Person()
print xiaoming
print xiaohong
print xiaoming == xiaohong
2018-10-05
import time
def performance(unit):
def per_decorator(f):
def fn(*args):
print('%s call...' % unit)
return f(*args)
return fn
return per_decorator
@performance('ms')
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1))
print factorial(10
def performance(unit):
def per_decorator(f):
def fn(*args):
print('%s call...' % unit)
return f(*args)
return fn
return per_decorator
@performance('ms')
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1))
print factorial(10
2018-10-04
import time
def performance(f):
def fn(x):
print("%4d-%02d-%02d %02d:%02d:%02d" % (time.localtime()[0:6]))
return f(x)
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):
print("%4d-%02d-%02d %02d:%02d:%02d" % (time.localtime()[0:6]))
return f(x)
return fn
@performance
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1))
print factorial(10)
2018-10-04
import time
def performance(f):
def fn(x):
print(time.asctime())
return f(x)
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):
print(time.asctime())
return f(x)
return fn
@performance
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1))
print factorial(10)
2018-10-04