调用变量时运行进程

时间:2017-02-05 05:18:12

标签: python python-3.x

当我点击thing.process对象时(当我循环遍历command.processCommand内的所有命令时),我想在defined[]内部运行代码,有没有办法实现这个目标?上述循环将在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)

eightball.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

1 个答案:

答案 0 :(得分:1)

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()