2
There is a practical way to check, how is written the code of any Python function, example max()
?
2
There is a practical way to check, how is written the code of any Python function, example max()
?
8
When it is a function written in Python, you can use the module inspect
.
For example:
def somar(a, b):
return a + b
In doing print(inspect.getsource(somar))
you will have the exit as string:
def somar(a, b):
return a + b
This will work for modules, classes, methods, functions and others; only it will not work for objects that have been implemented in C, such as the module sys
and the functions min
and max
. For this, you will need to look at the source files, at official repository.
If the intention is to thoroughly analyze what happens, I suggest using the module dis
. With it you can, for example, check the opcode which will be executed by the interpreter:
>>> print( dis.dis(somar) )
2 0 LOAD_FAST 0 (a)
2 LOAD_FAST 1 (b)
4 BINARY_ADD
6 RETURN_VALUE
None
What each command means you can read in the documentation.
3
All Python source code (standard implementation) is on Github and can be consulted. The role max()
.
Browser other questions tagged python function
You are not signed in. Login or sign up in order to post.