Adding code and methods to a class dynamically

Peter Hansen peter at engcorp.com
Fri Jul 29 03:54:10 CEST 2005


Sarir Khamsi wrote:
> Thanks, this is amazingly more simple than what I had. This works
> fine but I'm looking at the code for cmd and don't really see how you
> get completion to work on the new alias.

I'm not sure what "completion" means in this case, and I'm not aware of 
any "command-line completion" support in cmd.Cmd though it may well be 
there, so I can't say.  Certainly there is nothing in any way different 
about the new attribute created by the alias code, as both it and the 
original attribute are bound to exactly the same method.

> Also, I would like the alias to have arguments:
> alias newName existingName arg1 arg2 ...
> 
> Currying comes to mind, but I haven't tried it yet. Thanks again for
> the help.

It sounds like currying, but I'd suggest implementing such a thing with 
a completely separate layer on top of the existing Cmd stuff, probably 
through overriding .default() and doing a lookup in a dictionary, then 
building a command line with the original command and maybe executing it 
with .onecmd() if that can work from within the .cmdloop().  Lots of 
possibilities there; haven't tried any myself.

-Peter



More information about the Python-list mailing list