Julia 的“@edit”宏的 Python 等价物是什么?
What's the Python equivalent of Julia's `@edit` macro?
在 Julia 中,从 REPL 调用带有 @edit
宏的函数将打开编辑器并将光标置于定义方法的行。所以,这样做:
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 相同的等效 decorator/function?
免责声明:在 Python 生态系统中,这不是核心 language/runtime 的工作,而是 IDE 等工具的工作。例如,ipython shell has the ??
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 中的所有名称都是动态解析的,因此除非执行,否则绝大多数表达式都没有 well-defined 实现。调试器,例如pdb.set_trace()
提供,允许在执行时检查表达式。
在大多数 IDE 中,例如 PyCharm 或 VSCode,您可以按 Ctrl+ 单击一个函数/class 来获取它的定义,即使它是在核心语言或第 3 方中库(在 VSCode 中,这也适用于 Julia 顺便说一句。)。
限制是这仅适用于“纯 Python”代码,未显示 C 库代码等。
在 Julia 中,从 REPL 调用带有 @edit
宏的函数将打开编辑器并将光标置于定义方法的行。所以,这样做:
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 相同的等效 decorator/function?
免责声明:在 Python 生态系统中,这不是核心 language/runtime 的工作,而是 IDE 等工具的工作。例如,ipython shell has the ??
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 中的所有名称都是动态解析的,因此除非执行,否则绝大多数表达式都没有 well-defined 实现。调试器,例如pdb.set_trace()
提供,允许在执行时检查表达式。
在大多数 IDE 中,例如 PyCharm 或 VSCode,您可以按 Ctrl+ 单击一个函数/class 来获取它的定义,即使它是在核心语言或第 3 方中库(在 VSCode 中,这也适用于 Julia 顺便说一句。)。
限制是这仅适用于“纯 Python”代码,未显示 C 库代码等。