TypeError: 'list' object is not callable, though I am just passing a list to a function as an argument (Python)

TypeError: 'list' object is not callable, though I am just passing a list to a function as an argument (Python)

我目前正在编写一个使用强连接 类 并根据命令名称运行特定功能的框架。

在编写一个仅将一个列表和一个函数传递到另一个列表的函数时,我 运行 遇到了这个问题:

File "main.py", line 6, in <module>
    framework.module("modules.banner", console)
  File "C:\Users\Default\Desktop\Framework\framework\framework.py", line 53, in module
    new = importlib.import_module(module)
  File "C:\Program Files (x86)\Python38-32\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\Users\Default\Desktop\Framework\modules\banner.py", line 5, in <module>
    class Banner:
  File "C:\Users\Default\Desktop\Framework\modules\banner.py", line 41, in Banner
    event.commands(exit_console, commands)
TypeError: 'list' object is not callable

调用该函数的脚本:

...
class Banner:
...
    @event.event
    def on_ready():
        self.banner()
    
    def exit_console():
        print("3[1;32;0m")
        quit()

    command_names = ["exit", "quit", "e", "q"]
    event.commands(exit_console, command_names)
...

event.commands函数:

...
class event:
    def __init__(self):
        self.events = []
        self.commands = []
        self.parsers = []

    def event(self, function):
        self.events.append(function)
    def command(self, function):
        self.commands.append([function.__name__, function])
    def commands(self, function, lt):
        for name in lt:
            self.commands.append([name, function])
...

command和commands的区别是command取一个命令名,commands取多个。

event.command 示例:

...
@event.command
    def ping(args):
        print(args)
...

在此先感谢,我一直在寻找很多论坛,但找不到任何东西。

在您的 event class 中,您将 commands 定义为一个列表和一个函数。这就是导致错误的原因。

class event:
    def __init__(self):
        self.events = []
        self.commands = [] #list here
        self.parsers = []

    def event(self, function):
        self.events.append(function)
    def command(self, function):
        self.commands.append([function.__name__, function])
    def commands(self, function, lt): #function here
        for name in lt:
            self.commands.append([name, function])