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

标签 python julia read-eval-print-loop

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

julia> @edit 1 + 1
跳转到 julia/base/int.jl并将光标放在该行上:
(+)(x::T, y::T) where {T<:BitInteger} = add_int(x, y)
function form 一样:edit(+, (Int, Int))Python 中是否有与 Python REPL 相同的装饰器/函数?

最佳答案

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

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

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

关于python - Julia 的 `@edit` 宏的 Python 等价物是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64686950/

相关文章:

module - 在 Julia 的另一个模块中包含一个模块

julia - 在循环中更改变量 [Julia]

带有 repl 的 C++ IDE?

javascript - 如何要求全局使用 npm 安装模块?

clojure - 你能保存你的 Clojure REPL 的状态吗(或者,你能有效地使用 REPL 编写复杂的程序吗?)

python - setuptools 如何决定为 sdist/bdist 保留哪些文件?

python - 聪明的 any() 之类的函数来检查是否至少有 n 个元素为真?

arrays - 读入 Julia 中的数组

Python CIM_DataFile 按完整路径搜索文件

python - fbprophet库中的 'yhat'(预测)是如何计算的?