import time
def a(unit):
def d(f):
def fn(*args, **kw):
print '[%s] call %s()' % (unit, f.__name__)
return f(*args, **kw)
return fn
return d
@a('ms')
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1))
print factorial(10)
额。。。源码超300字了。。
def a(unit):
def d(f):
def fn(*args, **kw):
print '[%s] call %s()' % (unit, f.__name__)
return f(*args, **kw)
return fn
return d
@a('ms')
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1))
print factorial(10)
额。。。源码超300字了。。
2017-09-20
import os.path
print os.path.isdir(r'/data/webroot/resource/python') or True
print os.path.isfile(r'/data/webroot/resource/python/test.txt') or True
print os.path.isdir(r'/data/webroot/resource/python') or True
print os.path.isfile(r'/data/webroot/resource/python/test.txt') or True
2017-09-20
import time
def performance(f):
def fn(*args, **kw):
print time.time()
return f(*args, **kw)
return fn
@performance
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1))
print factorial(10)
系统报错:index.py 请检查输出是否匹配:call factorial() in,再试试!直接进入下一节
不是说打印时间嘛。。。
def performance(f):
def fn(*args, **kw):
print time.time()
return f(*args, **kw)
return fn
@performance
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1))
print factorial(10)
系统报错:index.py 请检查输出是否匹配:call factorial() in,再试试!直接进入下一节
不是说打印时间嘛。。。
2017-09-20
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()
2017-09-19
def gcd(x,y):
while y:
x,y=y,x%y
return x
借鉴上一节的运算加赋值语句,以此来表示辗转相除也是可以的
while y:
x,y=y,x%y
return x
借鉴上一节的运算加赋值语句,以此来表示辗转相除也是可以的
2017-09-19
def performance(unit):
def perform(f):
def perf(*args,**kw):
print 'call '+f.__name__+'() in '+time.strftime("%Y-%m-%d %H:%M:%S")
return f(*args,**kw)
return perf
return perform
def perform(f):
def perf(*args,**kw):
print 'call '+f.__name__+'() in '+time.strftime("%Y-%m-%d %H:%M:%S")
return f(*args,**kw)
return perf
return perform
2017-09-19
def calc_prod(lst):
def prod():
p = 1
for i in lst:
p *= i
return p
return prod
f = calc_prod([1, 2, 3, 4])
print f()
def prod():
p = 1
for i in lst:
p *= i
return p
return prod
f = calc_prod([1, 2, 3, 4])
print f()
2017-09-19
代码一模一样。玛德还报错 File "index.py", line 14
return '(Student :%s,%s,%s)' %
^
SyntaxError: invalid syntax
return '(Student :%s,%s,%s)' %
^
SyntaxError: invalid syntax
2017-09-19
这种首字符大写的还可以用capwords函数
import string
def change(s):
return string.capwords(s)
print map(change,['abc','TADG','tadgT'])
import string
def change(s):
return string.capwords(s)
print map(change,['abc','TADG','tadgT'])
2017-09-18
def is_sqr(x):
return x and x%math.sqrt(x)==0
print filter(is_sqr, range(1,101))
return x and x%math.sqrt(x)==0
print filter(is_sqr, range(1,101))
2017-09-18