?????????????????????????????????????????????
進階篇的裝飾器聽不懂的程序猿前來報道
?????????????????????????????????????????????
進階篇的裝飾器聽不懂的程序猿前來報道
?????????????????????????????????????????????
2017-04-03
def set_passline(passline):
def cmp(val):
if val>=passline:
print('pass')
else:
print('failed')
return cmp
func_100=set_passline(60)
func_150=set_passline(90)
func_100(89)
func_150(89)
返回的是cmp函數(shù),func_100=cmp,然后傳值再比較
def cmp(val):
if val>=passline:
print('pass')
else:
print('failed')
return cmp
func_100=set_passline(60)
func_150=set_passline(90)
func_100(89)
func_150(89)
返回的是cmp函數(shù),func_100=cmp,然后傳值再比較
2017-04-01
def route(path):
def decorator(func):
def wrapper(*args, **kwargs):
print "Path is: {}".format(path)
return func(*args, **kwargs)
return wrapper
return decorator
@route('/index.php')
def index(a, b):
print 'Index page will be show!', a, ":",b
def decorator(func):
def wrapper(*args, **kwargs):
print "Path is: {}".format(path)
return func(*args, **kwargs)
return wrapper
return decorator
@route('/index.php')
def index(a, b):
print 'Index page will be show!', a, ":",b
2017-03-23
#!/usr/bin/env python
# encoding: utf-8
x = 1
def foo():
x = 2
def innerfoo():
x = 3
print 'locals ', x
innerfoo()
print 'enclosing function locals ', x
foo()
print 'global ', x
運行結果:
locals 3
enclosing function locals 2
global 1
# encoding: utf-8
x = 1
def foo():
x = 2
def innerfoo():
x = 3
print 'locals ', x
innerfoo()
print 'enclosing function locals ', x
foo()
print 'global ', x
運行結果:
locals 3
enclosing function locals 2
global 1
2017-03-23
https://foofish.net/python-legb.html,學習返回函數(shù),閉包,裝飾器,必須先了解LEGB
2017-03-23
其實我覺得最重要的是返回函數(shù)的“”返回“”,定義一個函數(shù),返回的還是一個函數(shù),我們可以對主體函數(shù)進行賦值(不知道這樣說對不對),也可以對返回的函數(shù)進行賦值(這個是最好玩的地方),在主函數(shù)返回函數(shù)的時候,其相關參數(shù)和變量都保存在了返回的函數(shù)中,等待被調用!,如果返回函數(shù)不被調用,那主體函數(shù)永遠就只是fuction,不可能是輸出
(看完這個視頻,可以看看廖老師關于返回函數(shù)的那章,特別是最后關于閉包怎樣使用循環(huán)的那個,對python中f和f()的區(qū)別都能夠有很好的理解)
(看完這個視頻,可以看看廖老師關于返回函數(shù)的那章,特別是最后關于閉包怎樣使用循環(huán)的那個,對python中f和f()的區(qū)別都能夠有很好的理解)
2017-03-22
之前看教程 返回函數(shù)看不明白特別著急,老師一下就給我講通了,返回函數(shù),意味著函數(shù)返回的還是一個函數(shù),
就像是f_100= set_passline(60) 此時 f_100調用的就是返回的那個函數(shù),這個時候還可以對返回的那個函數(shù)進行輸入值...Amazing!!!
就像是f_100= set_passline(60) 此時 f_100調用的就是返回的那個函數(shù),這個時候還可以對返回的那個函數(shù)進行輸入值...Amazing!!!
2017-03-22