>
> I would *much *rather do this than use class methods anywhere in Leo.
> YMMV, but you have little chance of changing my mind. Yes, there is a bit
> more code involved, but it is completely encapsulated. In contrast,
> static/class methods have global consequences. I'm not going there.
>
>
>
This works without static/class methods and is a little less involved:
commands_dict = {}
ivars_dict = {}
def cmd(command_name):
class Decorator:
def __init__(self, func):
self.func = func
self.isMethod = '.' in func.__qualname__
commands_dict[command_name] = self.__call__
ivars_dict[command_name] = 'testClass'
def __call__(self, *args, **kwargs):
if self.isMethod:
return self.func(args[0], *args, **kwargs)
# args[0]: implicit 'self' of class method
else:
return self.func(*args, **kwargs)
return Decorator
@cmd('command1')
def pureFunction(event=None):
"""pureFunction docstring"""
print("pureFunction with argument:", event)
return ("...exit pureFunction")
class MyClass:
@cmd('command2')
def instanceMethod(self, event=None):
"""instanceMethod docstring"""
print("MyClass.instanceMethod with argument:", event)
return ("...exit instanceMethod")
for command in commands_dict:
func = commands_dict[command]
result = func('anArgument')
print("result:", result)
Reinhard
--
You received this message because you are subscribed to the Google Groups
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/leo-editor.
For more options, visit https://groups.google.com/d/optout.