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?
wait till you see
if __name__ = "__main__": main() `
Luckily Python is one step ahead:
Python 3.13.3 (main, Apr 22 2025, 00:00:00) [GCC 15.0.1 20250418 (Red Hat 15.0.1-0)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> if __name__ = "__main__": ... ... main() ... File "<python-input-0>", line 1 if __name__ = "__main__": ^^^^^^^^^^^^^^^^^^^^^ SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
yea I also couldnt get the formatting to work right, triple quotes kept turning things into accented letters, so I gave up.
and also := also known as the walrus operator is very fun and sometimes very convenient to use
I’ve always found needing to manually add a class instance parameter (i.e.
self
) to every object method really weird. And the constructors being named__init__
. Not having multiple dispatch is kinda annoying too. Needing to use decorators for class methods, static methods, and abstract classes is also annoying. Now that I think about it, Python kinda sucks (even though it’s the language I use the most, lol).Nah
self
is quite important. The main part of a method is to access the state of the object.self
is just the interface to it.Guess I just prefer languages that do it this way:
class AClass { var aProp = 0 fun aMethod() { aProp++ } }
Though I suppose confusion and bugs can happen when you do something like:
class AClass { var aProp = 0 fun aMethod(aProp: Int) { // `this.aProp` is needed to access the property } }
I would put my code in a
def main()
, so that the local names don’t escape into the module scope:if __name__ == '__main__': def main(): print('/s') main()
(I didn’t see this one yet here.)
I’m a little new to Python standards. Is this better or worse than putting the
def main():
outside the if statement (but callingmain()
inside it)I intended this an sarcastic example; I think it’s worse than putting the main outside of the branch because of the extra indent-level. It does have an upside that the
main()
doesn’t exist if you try import this as an module.I thought confusion about indent levels was the whole point of using python
But it feels like main function should not be indented
Sometimes I have the misfortune of working with python code written by someone else and I wonder how a language like this became anything more than a scripting language
The if block is where my arg parser goes
The
if
block is still in the global scope, so writing the code in it is a great way to find yourself scratching your head with a weird bug 30 minutes later.I call main() in the if
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
What’s the difference between a “scripting” language and a “real” one?
Scripting languages are real. Generally people consider dynamic languages scripting languages but it’s not that simple.
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.
Unix shell scripts are one of the few holdouts.
I don’t know if this applies to other shells, but bash will not only execute your script line-by-line, it will also read it line-by-line. Which means that you can modify the behavior of a running script by editing lines that have not yet been executed*. It’s absolutely bonkers, and I’m sure that it has caused more than one system failure, during upgrades.
* For example, if you run the following script
echo "hello" sleep 5 echo "goodbye"
and then edit the third line before the 5 second sleep has elapsed, then the modified line will be executed.
I have run into the problem of modifying a bash script while it is running.
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.
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.)
procedural programming is more akin to that, but python has far to many oop concepts to be considered procedural imo
Procedural and OOP aren’t mutually exclusive terms. Most OOP programs are ultimately procedural in nature. Often, the only difference is that the first argument to the function is to the left the function name and separated by a dot.
fair, I just think it’s misleading to call python procedural, but it lines up with what the commenter above was describing and searching for the term for
I’d say the term “procedural” itself is an issue. Pretty much any language can be done that way if you choose. IIRC, the creator of Clojure wanted Java to work more that way, and he did it by having a single class full of functions. It’s not a natural way to write Java, and that’s why he invented Clojure.
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
compared with other languages at the time, the ease of access and readability makes it worth it. plus, the heavy duty stuff is usually handled by more optimised code line numpy or sklearn…
Readability? Me eyes bleed from a day of partially staring at python code, and there is a whole another week of that ahead. Tzinch (Edit: Tzeentch) help me
Like in every programming language, it depends who wrote the code. OK, *nearly every programming language, see: LISP.
You can write cryptic, write-only programs in about any language, but you can even write readable and maintainable PERL scripts (despite people claiming this to be impossible).
As much as I am inclined to agree with this, still can’t
see: LISP
Also, see: Python with more than three lines of logic. I could suspect that’s just the me-versus-whitespaces thing, but no, YAML files do not get me dizzy in under thirty seconds of reading. Van Rossum made a huge miscalculation here
Everyone’s welcome to their opinion of course, but I find Python more readable than anything else and I resent the visual clutter required to make intentions plain in other languages. Feels like having a conversation where people say the words “comma”, “period”, etc.
I also spend more time with Python than anything else and I suspect these two facts about me relate, lol
Someone should get their hands on someone like me and someone like you and study their brains. I spend most time with PHP and C++, and Python looks like an attempt to write code like prose literature. Very interesting how much of this is habbit, as it can’t be just that: reading prose and poetry in English/Russian/Japanese never produced this kind of resentment
I would love that! I do think there are probably interesting underlying personality factors / preferences for a lot of this stuff as well.
I do think that many of Python’s characteristics map to my own personality and I bet there’s something to that. Things like syntax of course, but not strictly syntax, also things like “The Zen of Python”, and the way its a “jack-of-all-trades, master-of-none”. I also really kind of need the freedom and accompanying responsibility to break any “rules” on a whim (Python will happily let you overwrite its own internals while running, for instance), but I almost never do anything that uses it…
I could probably keep going lol. Feels like a “people looking like their pets” scenario, lmao
Python people explaining fail to see the point: Yes we know dunders exist. We just want you to say: “Yeah, that is a bit hacky, isn’t it?”
Tbh reserving “main” is just a hacky if not more so than checking
__name__
if you actually understand language design.Both are indeed equally terrible, even if it is for different reasons.
The one true choice is of course letting the programmer choose the main function in compile/interpretation-time.
I.e.
python main.py --start "main"
would start by calling the main function in main.pyMost contemporary python tools like flask or uvicorn do exactly this and require an explicit entry point
Reserving
main
is definitely more hacky. Try compiling multiple objects withmain
defined into a single binary - it won’t go well. This can make a lot of testing libraries rather convoluted, since some want to write their ownmain
while others want you to write it because require all kinds of macros or whatever.On the other hand,
if __name__ == "__main__"
very gracefully supports having multiple entrypoints in a single module as well as derivative libraries.What is not hacky then in a language design?
Letting the developer decide what the code should do.
I don’t understand. What do you mean by deciding what the code should do in the context of language design? Can you give a concrete example? I am confused because the “main” function is required when you make an executable. Otherwise, a library will not contain any main function and we could compile it just fine no? (Shared library)
Python is an interpreted language that doesn’t need a main function explicitly. You can define any package entry points you want at the package config level. (setup.py, etc)
example: What I meant was I prefer language that treat developers like adults. If I want ptrhon’s “ux” to hide some functions or objects I can do that with underscores, but nothing is private, a developer using my library can do whatever they want with it, access whatever internals they want (at their own risk of course)
Is it? I really don’t think so. What can you propose that’s better? I think
if __name__ == __main__
works perfectly fine and can’t really think of anything that would be better.And you don’t have to use it either if you don’t want to anyway, so no, I don’t think it’s that much of a hack. Especially when the comic compares C as an example, which makes no sense to me whatsoever.
aren’t most of not all conventions hacky anyways?
if debug.getinfo(1).what == "main" then -- ... end
Not that you’ll ever use it. No, seriously.
Edit: actually, they are not quite equivalent. This code just checks whether we are outside any function, not necessarily in the main file (i.e. not in a module). I don’t think there’s an equivalent to Python’s
__name__
in stock Lua.Depends on how lazy I am at the moment.
main.py
or did you not read the manual?Could someone explain this please? I’m still a noob.
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.checks username
So it’s you they’re always talking about
It is, it’s the other Barry.
Really helpful explanation, thanks.
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.thats why i name my modules main.py
Really helpful explanation, thanks.
Nothing prevents you from putting a call to “main()” in the global scope
The point of the name==main logic is that it checks if that is the file that was invoked (like running
python filename.py
). If you just put a main() in the global scope it will be called either when the file is invoked or loaded (which can cause unintended consequences).Dumb person question: if it’s good practice to do this so things don’t go sideways, shouldn’t it be a built-in feature/utility/function/whatever?
It is “built-in” as the name is part of python. However, Python runs top to bottom, rather than having a special entrypoint. So name is just a utility you can use in your design.
While it can be a good practice to define a main entrypoint, that’s more of a design decision and not hard rule. Many applications would not benefit from it because there is only one way to actually call the application to begin with.
Edit: Also not a dumb question. All programming languages have unique elements to them due to how they were envisioned and or developed over time (Pythons 30 years old)
I really appreciate the explanation!
Just cross your fingers nobody attempts to import it…