Do you stay away from C++ too? You can do this there too
https://en.cppreference.com/w/cpp/utility/source_location/line
Do you stay away from C++ too? You can do this there too
https://en.cppreference.com/w/cpp/utility/source_location/line
This stuff is normally used for creating human readable error messages. E.g. printing the line of your code that actually set off the exception
The add
function in the example above probably traverses the call stack to see what line of the script is currently being executed by the interpreter, then reads in that line in the original script, parses the comment, and subs in the values in the function call.
This functionality exists so when you get a traceback you can see what line of code triggered it in the error message
You should look at how OPs example works first maybe
The python interpreter isn’t parsing comments, the add() function is just getting the current line number from the call stack context, and using a regex to spit out the numbers to the right of the “#” on the current executing line of the source code.