Python needs an actual default function
-
Tbh reserving "main" is just a hacky if not more so than checking
__name__
if you actually understand language design.Yeah, this is it.
What's hacky about an introspective language providing environment to all of the executing code, so that the coder can make the decision about what to do?
It would by hacky if Python decided "We'll arbitrarily take functions named "main" and execute them for you, even though we already started execution at the top of the file."
For C, this is less so. The body of the file isn't being executed, it's being read and compiled. Without a function to act as a starting point, it doesn't get executed.
-
Just cross your fingers nobody attempts to import it...
Due to the oneness of all things, I refuse to distinguish between library code and executable code. One and Zero are arbitrary limitations.
-
Could someone explain this please? I'm still a noob.
wrote on last edited by [email protected]All code needs to have an entry point.
For Python and some other languages, this is the start of the file.
For other languages, this is a special function name reserved for this purpose - generally, "main".
In the first kind of language, the thought process is basically: I have the flow of execution, starting at the top of the file. If I want to make a library, I should build the things I want to build, then get out of the way.
In the other kind of language, the thought process is basically: I am building a library. If I want to make an executable, I should create an entry point they the execution starts at.
The debate is honestly pretty dumb.
-
Basically, when you compile a program written in Rust or C/C++ (the first and second panels respectively), the compiler needs to know what's supposed to be executed first when the program is run directly (i.e. when you click on the executable), which in these languages, is denoted by a special function called
main()
. Executable files can also contain functions and data structures that can be called by other programs, and when they are, you wouldn't want to run an entire complex and resource intensive program if another program only needs to call a single function from it. In that case, the other program will call the function it wants but not main, so only that function executes and not the entire program.However, Python is a scripting language that's interpreted. So every Python source file is executable provided you have the Python runtime. Python also doesn't have native support for main functions in the same way Rust and C/C++ does, and it will execute every line of code as it reads the source file. This is why a single line Python file that just calls print is valid, it doesn't need to be wrapped in a main function to execute. However, what if your Python file is both meant to be executed directly and provides functions that other Python files can call? If you just put the main routine in the root of the file, it would be executed every time another program tries to import the file in order to call functions from it, since the import causes the file to be interpreted and executed in its entirety. You can still just have a main function in your file, but since Python doesn't natively support it, your main function won't do anything if you run the file directly because as far as Python is concerned, there is no executable code at the root of the file and you haven't called any functions.
The workaround is to have a single if statement at the root of the file that looks like this:
if __name__ == '__main__': main()
It checks a special variable called
__name__
. If the Python file is directly executed,__name__
will have the value of the string'__main__'
, which satisfies the if statement so main() is called. If another Python file imports it, the value of__name__
will be the name of that file, so main() isn't called. It's clunky and not that efficient, but, 1, it works, and 2, if you cared about efficiency, you wouldn't be writing it in Python.Really helpful explanation, thanks.
-
Python has a bunch of magic variables, like
__name__
. This one contains the name of the module you're currently in (usually based on the file name), so if your file is calledfoo.py
, it will have the valuefoo
.But that's only if your module is being imported by another module. If it's executed directly (e.g.
python foo.py
), it will instead have a__name__
of__main__
. This is often used to add a standalone CLI section to modules - e.g. the module usually only defines functions that can be imported, but when executed it runs an example of those functions.Really helpful explanation, thanks.
-
I call main() in the if
I always use
if "__main__" == main: __main__()
..and earlier in the code:
def __main__(): while True: pass main = "__main__"
This helps to prevent people from arbitrarily running my code as a library or executable when I don't went them to.
-
I always use
if "__main__" == main: __main__()
..and earlier in the code:
def __main__(): while True: pass main = "__main__"
This helps to prevent people from arbitrarily running my code as a library or executable when I don't went them to.
-
I work in an academic / research environment. Depending who wrote it, even seeing a
__name__ == "__main__"
is a bit of a rare thing...python isn't the only language to do "execute everything imported from a particular file and all top level statements get run". both node and c# (but with restrictions on where top level statements can be) can do that type of thing, I'm sure there's more.
python conventions are unique because they attempt to make their entrypoint also importable itself without side effects. almost no one needs to do that, and I imagine the convention leaked out from the few people that did since it doesn't hurt either.
for instance in node this is the equivalent, even though I've never seen someone try before:
if (path.resolve(url.fileURLToPath(import.meta.url)).includes(path.resolve(process.argv[1]))) { // main things }
-
Sounds like a skill issue on your end
Agreed. I program mainly in C so its easier for me to make sense of bad C code than bad python code which just makes me cry
-
It really doesn't. It's a scripting language, functions are there but at it's core it runs a script. The issue is that it was so easy to start with that people started doing everything in it, even though it sucks for anything past complex scripts
It is the excel of databases.
Excel recently added the ability to run python code lol
-
Since Java 21, this has been shortened significantly. https://www.baeldung.com/java-21-unnamed-class-instance-main
Only took 27 years to make the Java "Hello, world!" kinda sane.
-
It really doesn't. It's a scripting language, functions are there but at it's core it runs a script. The issue is that it was so easy to start with that people started doing everything in it, even though it sucks for anything past complex scripts
It is the excel of databases.
What's the difference between a "scripting" language and a "real" one?
-
What's the difference between a "scripting" language and a "real" one?
I didn't say it wasn't real, it's just a scripting structure and not object oriented, so it doesn't make sense for it to start by looking for a "main" object
-
I didn't say it wasn't real, it's just a scripting structure and not object oriented, so it doesn't make sense for it to start by looking for a "main" object
What would make it "object oriented"?
-
What would make it "object oriented"?
You know what, I take that back:
https://docs.python.org/3.13/faq/general.html#what-is-python -
Not having tons of code in one if statement, but in a function.
And scope. Variables declared in the if can be read everywhere, variables declared in the function are limited to that function.
-
How do you feel about other peoples Go code?
Go code is always an abomination.
-
I didn't say it wasn't real, it's just a scripting structure and not object oriented, so it doesn't make sense for it to start by looking for a "main" object
not object oriented
I don't think we have a name for what you are trying to say here.
(And yeah, "object oriented" isn't it.)
-
What's the difference between a "scripting" language and a "real" one?
It's a scripting language. What means that the computer runs it line by line, without needing to get the entire project first.
-
It's a scripting language. What means that the computer runs it line by line, without needing to get the entire project first.
That is not how Python works. There are very few languages that work by executing line-by-line anymore. Unix shell scripts are one of the few holdouts. JavaScript also does it to a certain extent; the browser starts executing line-by-line while a compiler step works in the background. Once the compiler is done, it starts execution of the compiled form right where the line-by-line execution left off. It helps JavaScript be more responsive since it doesn't have to wait for the compiler to finish.