Python needs an actual default function
-
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.
-
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.
Can you elaborate on this blood magic?
-
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.)
procedural programming is more akin to that, but python has far to many oop concepts to be considered procedural imo
-
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.
Python still has the
-i
option, and it still runs the same language as the files interface. -
Python still has the
-i
option, and it still runs the same language as the files interface.The
-i
option is simply interactive mode. All commands still go through a compiler. -
Also, do y'all call main() in the if block or do you just put the code you want to run in the if block?