gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 17:15:26 25 4
gpt4 key购买 nike

在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com