Julia 的 `@edit` 宏的 Python 等价物是什么?

Jen*_*sun 8 python read-eval-print-loop julia

在 Julia 中,使用@editREPL 中的宏调用函数将打开编辑器并将光标放在定义方法的行上。所以,这样做:

julia> @edit 1 + 1
Run Code Online (Sandbox Code Playgroud)

跳转到julia/base/int.jl并将光标放在该行上:

(+)(x::T, y::T) where {T<:BitInteger} = add_int(x, y)
Run Code Online (Sandbox Code Playgroud)

一样的函数形式edit(+, (Int, Int))

Python 中是否有与 Python REPL 相同的装饰器/函数?

Mis*_*agi 5

免责声明:在 Python 生态系统中,这不是核心语言/运行时的工作,而是 IDE 等工具的工作。例如,ipython shell具有??特殊的语法来获得改进的帮助,包括源代码。

Python 3.8.5 (default, Jul 21 2020, 10:42:08)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.18.1 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import random

In [2]: random.uniform??
Signature: random.uniform(a, b)
Source:
    def uniform(self, a, b):
        "Get a random number in the range [a, b) or [a, b] depending on rounding."
        return a + (b-a) * self.random()
File:      /usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/random.py
Type:      method
Run Code Online (Sandbox Code Playgroud)

Python运行时自身允许查看源代码的对象通过inspect.getsource。这使用启发式搜索可用的源代码;对象本身不携带其源代码。

Python 3.8.5 (default, Jul 21 2020, 10:42:08)
[Clang 11.0.0 (clang-1100.0.33.17)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import inspect
>>> print(inspect.getsource(inspect.getsource))
def getsource(object):
    """Return the text of the source code for an object.

    The argument may be a module, class, method, function, traceback, frame,
    or code object.  The source code is returned as a single string.  An
    OSError is raised if the source code cannot be retrieved."""
    lines, lnum = getsourcelines(object)
    return ''.join(lines)
Run Code Online (Sandbox Code Playgroud)

不可能将任意表达式或语句解析为它们的来源;由于 Python 中的所有名称都是动态解析的,因此除非执行,否则绝大多数表达式都没有明确定义的实现。调试器(例如由 提供pdb.set_trace())允许在表达式执行时对其进行检查。