Skip to content
Advertisement

What’s the Python equivalent of Julia’s `@edit` macro?

In Julia, calling a function with the @edit macro from the REPL will open the editor and put the cursor at the line where the method is defined. So, doing this:

JavaScript

jumps to julia/base/int.jl and puts the cursor on the line:

JavaScript

As does the function form: edit(+, (Int, Int))

Is there an equivalent decorator/function in Python that does the same from the Python REPL?

Advertisement

Answer

Disclaimer: In the Python ecosystem, this is not the job of the core language/runtime but rather tools such as IDEs. For example, the ipython shell has the ?? special syntax to get improved help including source code.

JavaScript

The Python runtime itself allows viewing source code of objects via inspect.getsource. This uses a heuristic to search the source code as available; the objects themselves do not carry their source code.

JavaScript

It is not possible to resolve arbitrary expressions or statements to their source; since all names in Python are resolved dynamically, the vast majority of expressions does not have a well-defined implementation unless executed. A debugger, e.g. as provided by pdb.set_trace(), allows inspecting the expression as it is executed.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement