In der Methode `command` müsste es so heißen:
Und mache den `:` hinter `@command` weg.
Aber das wird nicht funktionieren, weil Python anders als Ruby keine ausführbaren Klassenrümpfe im Kontext des Klassenobjekts hat, sondern wenn der Rumpf von `Command` ausgeführt wird, gibt es diese Klasse noch gar nicht. Ziehe das als Funktion raus.
Code: Alles auswählen
commands = {}
def command(name):
def add_command(func):
commands[name] = func
return func
return add_command
class Command(object):
@command('say')
def say(self, args): pass
print commands
Update: DasIch hat Recht. Dies geht auch:
Code: Alles auswählen
class Command(object):
commands = {}
def command(name, c=commands):
def add_command(func):
c[name] = func
return func
return add_command
@command('say')
def say(self, args): pass
print Command.commands
Stefan