1

In Bash, if I create a function, I can use the type command to show me that function definition like so:

# define a function
foo {
    echo "bar"
}

# now show me that definition
$ type foo
foo is a function
foo ()
{
    echo "bar"
}

Zsh and Fish both have something similar with their functions commands. I checked the bash-to-xsh page here, but could not find an equivalent. I tried using Python's inspect, but that didn't work either:

def foo():
  echo "bar"

$ foo
<function __main__.foo>

$ import inspect as i
$ i.getsource(foo)
xonsh: For full traceback set: $XONSH_SHOW_TRACEBACK = True
OSError: could not get source code

How do you show method definitions in Xonsh?

mattmc3
  • 16,761
  • 6
  • 81
  • 99

1 Answers1

0

It appears that this is a limitation of Python, and not really a problem with Xonsh, according to this thread.

If the Xonsh function is defined in a file, inspect works. If the function is defined only in memory, it is not available.

For example, this code works:

import inspect
echo @(inspect.getsource($PROMPT_FIELDS['cwd']))

So does this:

import inspect
echo "def foo():\n\techo 'bar'\n" > foo.xsh
source foo.xsh
echo @(inspect.getsource(foo))
mattmc3
  • 16,761
  • 6
  • 81
  • 99