1 回答

TA貢獻(xiàn)1893條經(jīng)驗(yàn) 獲得超10個(gè)贊
你不能,至少你的裝飾器的編寫(xiě)方式是這樣。當(dāng)你裝飾一個(gè)函數(shù)時(shí),它類似于:
def execute_multi_commands(command, count):
LOG.info(f'Executing command {count}: {command}')
os.system(command)
count += 1
execute_multi_commands = read_commands(execute_multi_commands)
所以在這之后,read_commands已經(jīng)被執(zhí)行了,并且文件已經(jīng)被讀取了。
您可以做的是更改裝飾器以讀取包裝器中的文件,例如:
def read_commands(inner, path=BATCH_PATH):
def wrapper(*args, **kwargs):
if "path" in kwargs:
path_ = kwargs.pop("path")
else:
path_ = path
with open(path_) as f:
commands = ['python ' + line.replace('\n', '') for line in f]
for command in commands:
inner(command, *args, **kwargs)
return wrapper
...但這意味著每次調(diào)用修飾函數(shù)時(shí)都會(huì)讀取該文件,這與您之前所做的略有不同。
添加回答
舉報(bào)