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:
julia> @edit 1 + 1
jumps to julia/base/int.jl
and puts the cursor on the line:
(+)(x::T, y::T) where {T<:BitInteger} = add_int(x, y)
As does the function form: edit(+, (Int, Int))
Is there an equivalent decorator/function in Python that does the same from the Python REPL?
Julia codes can easily be made by converting C or Python codes. It is very difficult to make Python codes by converting C codes or the other way around. Julia arrays are 1-indexed, i.e. arrays start with 1-n not 0-n. It might cause a problem with programmers having habit of using other languages.
Macro is one of the ways to evaluate the input expression and gives the resultant output expression. The process in the Julia language works as follows: it first parses and evaluates the macro, and the processed code produced by the macro is eventually evaluated like an ordinary expression. Macro can be referred to as a code of code.
Julia started development in 2009 and was first released to the public as Julia 1.0 in 2012. Although it is yet to have a big fanbase as indicated by the TIOBE index, Python is the third most popular language after C and Java while Julia ranks somewhere in the 40s, it is still worth a visit.
In order to figure out the types to pass to an IAI function from the Python interface, you can refer to the equivalent function in the Julia API and translate the types to their Python equivalent. Most literal data types convert in a straighforward manner, for example:
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.
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/[email protected]/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/random.py
Type: method
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.
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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With