调用变量时运行进程

2024-09-30 08:17:03 发布

您现在位置:Python中文网/ 问答频道 /正文

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

command.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)

八球

    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

Tags: namepyimportselfmessagedefargsdescription
1条回答
网友
1楼 · 发布于 2024-09-30 08:17:03
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()

相关问题 更多 >

    热门问题