Adding code and methods to a class dynamically

Sarir Khamsi sarir.khamsi at raytheon.com
Thu Jul 28 19:07:23 EDT 2005


Peter Hansen <peter at engcorp.com> writes:

> If it's truly just an alias you want, then something like this should
> work better.  Replace everything in the if mo: section with this:
>
> if mo:
>      newName = mo.group(1)
>      existingName = mo.group(2)
>      existingMethod = getattr(self, 'do_' + existingName)
>      setattr(self, 'do_' + newName, existingMethod)

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.

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.

Sarir



More information about the Python-list mailing list