http://www.jb51.net/article/65009.htm 看不懂可以去看看这个,结合前面讲的返回函数就能理解了
2015-07-29
要记得python里面对于变量名认为是引用,经过装饰器后的函数名,简单可以认为是指向到新的一个函数去(这样并不是正确的),所以没通过functools.warp()对原函数的内置属性复制,那么通过旧名(其实已经是一个新函数,只是新函数名还是原来旧的),那么旧名所表现出来的属性便是新函数的
2015-07-29
def One(func):
def Two(*args,**kw):
return func(*args,**kw)
return Two
@One
def test():
return 'this is a test'
test
test()
对函数test加了@One,等于函数One(test),其实只看函数One的返回值就行,返回一个Two的函数型
那么使用test后,则等同test = Two函数型,那test()类推,则是一个func(*args,*kw)类型,而func是函数One
的参数,也就是最开始的传入的test,只是被One修改过
def Two(*args,**kw):
return func(*args,**kw)
return Two
@One
def test():
return 'this is a test'
test
test()
对函数test加了@One,等于函数One(test),其实只看函数One的返回值就行,返回一个Two的函数型
那么使用test后,则等同test = Two函数型,那test()类推,则是一个func(*args,*kw)类型,而func是函数One
的参数,也就是最开始的传入的test,只是被One修改过
2015-07-29
import time
def performance(unit):
def performance_new(f):
def performance_lst(*args,**kw):
print '[%s]call %s()...'%(unit,f.__name__)
print time.asctime()
return f(*args,**kw)
return performance_lst
return performance_new
def performance(unit):
def performance_new(f):
def performance_lst(*args,**kw):
print '[%s]call %s()...'%(unit,f.__name__)
print time.asctime()
return f(*args,**kw)
return performance_lst
return performance_new
2015-07-29
import os
print os.path.isdir(r'/data/webroot/resource/python')
print os.path.isfile(r'../../../../../../../../../../../.././etc/passwd')
cmd = "dir ./../../../../../../../../../../../.././etc/"
for i in os.popen(cmd).readlines():
print i
print os.path.isdir(r'/data/webroot/resource/python')
print os.path.isfile(r'../../../../../../../../../../../.././etc/passwd')
cmd = "dir ./../../../../../../../../../../../.././etc/"
for i in os.popen(cmd).readlines():
print i
2015-07-29
class Fib(object):
def __call__(self,num):
L=[0,1]
for i in range(num-2):
L.append(L[-1]+L[-2])
return L
f = Fib()
print f(10)
def __call__(self,num):
L=[0,1]
for i in range(num-2):
L.append(L[-1]+L[-2])
return L
f = Fib()
print f(10)
2015-07-26
起初想的:
def is_sqr(x):
return math.modf(math.sqrt(x))[0] == 0
其实化繁为简就好了,自用math.sqrt()就能实现:
def is_sqr(x):
return math.sqrt(3) % 1 == 0
def is_sqr(x):
return math.modf(math.sqrt(x))[0] == 0
其实化繁为简就好了,自用math.sqrt()就能实现:
def is_sqr(x):
return math.sqrt(3) % 1 == 0
2015-07-26
class Fib(object):
def __init__(self, num):
L=[0,1]
for i in range(num-2):
L.append(L[-1]+L[-2])
self.names=L
def __str__(self):
return str(self.names)
def __len__(self):
return len(self.names)
def __init__(self, num):
L=[0,1]
for i in range(num-2):
L.append(L[-1]+L[-2])
self.names=L
def __str__(self):
return str(self.names)
def __len__(self):
return len(self.names)
2015-07-25
def __cmp__(self, s):
if self.score>s.score:
return -1
if self.score<s.score:
return 1
return cmp(self.name,s.name)
if self.score>s.score:
return -1
if self.score<s.score:
return 1
return cmp(self.name,s.name)
2015-07-25