gpt4 book ai didi

python - 调用变量时运行进程

转载 作者:太空宇宙 更新时间:2023-11-03 15:41:48 25 4
gpt4 key购买 nike

当我点击 command.processCommand 对象时(当我循环遍历放置在 thing.process 中的所有命令时,我想运行放置在 thing.process 中的代码define[]),有什么方法可以实现这一点吗?上述循环将像 myproject.py 中一样执行

命令.py

class Command:
global defined

defined = []

def __init__(self, name):
self.name = name
self.description = "This command lacks a description"
self.args = ""
self.process = None

defined.append(self)

八球.py

    def processCommand():
print('hello')

thing = commands.Command('8ball')
thing.description = "Gives you a response from the mighty 8ball."
thing.process = processCommand

myproject.py

# Cogs
import cogs.commands as commands
import cogs.eightball
import cogs.helloworld

def processCommands(message):
if(message.content[:2] == "b#"):
args = message.content.split(' ')

args[0] = args[0][2:]

for command in defined:
if args[0] == command.name:
command.args = args
command.processCommand

最佳答案

for x in defined: 
if x.process: # to skip `self.process = None`
x.process()
<小时/>

编辑:您需要process()而不是processCommand

    for command in defined:
if args[0] == command.name:
command.args = args
command.process()

关于python - 调用变量时运行进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42048783/

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