gpt4 book ai didi

python - 以相同的名称调用不同的函数,具有相同的 Action

转载 作者:行者123 更新时间:2023-12-04 14:05:32 29 4
gpt4 key购买 nike

我想知道如何向已运行的代码添加新行(它甚至可以是一个简单的 while 循环)并运行它。但是这种创建和导入类或函数的方法总是运行添加的第一行代码,即使您更改了函数并再次导入它也是如此。我尝试添加 del main,并将行 data = main(data) 替换为 data = main(data).copy(),但是两者都没有给出任何结果。没有出现任何错误或日志,我在步骤中添加了信息以供理解。

代码:

def applyCommand(data, command):
with open('/$PATH/simple_macro.py', 'w') as file:
file.write(command)
print('[INFO] simple macro has been written- step 1 ✓.')

from simple_macro import main
print('[INFO] simple macro has been imported - step 2 ✓.')

data = main(data)
print('[INFO] data has been updated - step 3 ✓.')

from os import remove
remove('/$PATH/simple_macro.py')
print('[INFO] simple macro file has been deleted - step 4 ✓.')

return data

data = list(range(10))

command = "def main(data):\n return data[:5]"
data = applyCommand(data, command)
print("Updated data:", data)

commandTwo = "def main(data):\n return data[0]"
data = applyCommand(data, commandTwo)
print("Updated data:", data)

输出:

[INFO] simple macro has been written- step 1 ✓.
[INFO] simple macro has been imported - step 2 ✓.
[INFO] data has been updated - step 3 ✓.
[INFO] simple macro file has been deleted - step 4 ✓.
Updated data: [0, 1, 2, 3, 4]
[INFO] simple macro has been written- step 1 ✓.
[INFO] simple macro has been imported - step 2 ✓.
[INFO] data has been updated - step 3 ✓.
[INFO] simple macro file has been deleted - step 4 ✓.
Updated data: [0, 1, 2, 3, 4]

第一部分产生正确的预期输出,但第二部分的预期输出只是 0 但实际输出是 [0, 1, 2, 3, 4] 如你所见。所以问题是可以对该代码单元进行什么类型的添加或更改,以便在第一次调用后获得预期的输出?

最佳答案

在单次运行中运行、导入和更改相同的函数和程序,将在您的计算机内存中留下最初创建的 main()。为了正确刷新导入,您需要使用 importlib 模块中的 reload():

from importlib import reload
import os
def applyCommand(data, command):
with open('simple_macro.py', 'w') as file:
file.write(command)
print('[INFO] simple macro has been written- step 1 ✓.')

import simple_macro
reload(simple_macro)
with open('simple_macro.py') as file:
print(file.read())
print('[INFO] simple macro has been imported - step 2 ✓.')

data = simple_macro.main(data)
print('[INFO] data has been updated - step 3 ✓.')

os.remove('simple_macro.py')
print('[INFO] simple macro file has been deleted - step 4 ✓.')

return data

data = list(range(10))

command = "def main(data):\n return data[:5]"
data = applyCommand(data, command)
print("Updated data:", data)

commandTwo = "def main(data):\n return data[0]"
data = applyCommand(data, commandTwo)
print("Updated data:", data)

结果:

[INFO] simple macro has been written- step 1 ✓.
def main(data):
return data[:5]
[INFO] simple macro has been imported - step 2 ✓.
[INFO] data has been updated - step 3 ✓.
[INFO] simple macro file has been deleted - step 4 ✓.
Updated data: [0, 1, 2, 3, 4]
[INFO] simple macro has been written- step 1 ✓.
def main(data):
return data[0]
[INFO] simple macro has been imported - step 2 ✓.
[INFO] data has been updated - step 3 ✓.
[INFO] simple macro file has been deleted - step 4 ✓.
Updated data: 0

我添加了对 simple_macro 所做的实际更改的打印。

关于python - 以相同的名称调用不同的函数,具有相同的 Action ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68649144/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com