xonsh: Clash between alias and function with same name

201 Views Asked by At

I've this function defined in my xonshrc:

def turn_into_alias(func, keep=False):
    aliases[func.__name__] = func
    del func

It's supposed to convert a function into an alias. But it doesn't work. It adds the alias all right but the deleting doesn't work, because it deletes the function reference passed to it.

Is it possible to setup xonsh so that it gives higher priority to the aliases? (probably not, because it's python after all, in that case:)

Is there a way to achieve the effect I want in xonsh?

1

There are 1 best solutions below

0
On

In Python, and thus xonsh, del only modifies the current scope. So in your example, you are really just deleting the name func inside of turn_into_alias(). The function object may still have references to it elsewhere so Python won't delete it.

Since this is in your xonshrc, probably the simplest thing to do would be to delete it from globals() or the __xonsh_ctx__.

Something like the following should work

def turn_into_alias(func, keep=False):
    name = func.__name__
    aliases[name] = func
    del globals()[name]

or

def turn_into_alias(func, keep=False):
    name = func.__name__
    aliases[name] = func
    del __xonsh_ctx__[name]