Is there a way to get the current line in the source file you are on, like __LINE__
does in C++?
Use readlines() to get Line Count This is the most straightforward way to count the number of lines in a text file in Python. The readlines() method reads all lines from a file and stores it in a list. Next, use the len() function to find the length of the list which is nothing but total lines present in a file.
The new line character in Python is \n . It is used to indicate the end of a line of text. You can print strings without adding a new line with end = <character> , which <character> is the character that will be used to separate the lines.
The standard solution to find the current line number in C++ is using the predefined macro __LINE__ . It returns the integer value representing the current line in the source code file being compiled. The preprocessor will simply replace the __LINE__ macro with the current line number.
LINE The presumed line number (within the current source file) of the current source line (an integer constant). As an integer constant, code can often assume the value is __LINE__ <= INT_MAX and so the type is int . To print in C, printf() needs the matching specifier: "%d" .
Yep, you can use __LINE__
. Also, __FILE__
.
See Keywords section
As BCS and Jonathan M Davis point out in the comments, there is a special case for __LINE__
and friends: when used as the default value of a template or function argument, they resolve to the location of the caller, not the signature of the template or function. This is great for saving callers from having to provide this information.
void myAssert(T)(lazy T expression, string file = __FILE__, int line = __LINE__)
{
if (!expression)
{
// Write the caller location
writefln("Assert failure at %s:%s", file, line);
}
}
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