wait till you see
if __name__ = "__main__":
main()
`
Welcome to Programmer Humor!
This is a place where you can post jokes, memes, humor, etc. related to programming!
For sharing awful code theres also Programming Horror.
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
What kind of psychopath would put the code in the if block.
Looks at all the Python scripts in my bin folder that I wrote.
Never heard of
def main():
pass
if __name__ == '__main__':
main()
?
I remember how weird this looked the first time I saw it and while I may now understand it, it still looks jank af
Heard of it, was too lazy to do it that way.
To be fair I now do it that way, but not when I was learning Python.
I work in an academic / research environment. Depending who wrote it, even seeing a __name__ == "__main__"
is a bit of a rare thing...
Academic code is absolutely horrific.
Fortunately, it is possible to translate it for practical applications.
As someone in academia who writes code, I can confirm.
Do you also have nothing but love for those 50+ cell Jupyter notebooks that don't use a single function and have everything in the global scope?
the best thing is when not even the author knows the correct order of running the cells; because of course it isn't top-to-bottom.
Yeah, and also zero dependency management, so you are free to figure out what combination of Python, Tensorflow and Keras will make it not throw random exceptions.
And don't forget the number one rule: you must use all the graphing libraries, all the time.
Why would you waste a function call on something so completely redundant?
~For real though, arg parsing goes in the if, then gets dispatched to whatever function call is needed to run the proper script.~
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
I feel that Python is a bit of a 'Microsoft Word' of languages. Your own scripts are obviously completely fine, using a sensible and pragmatic selection of the language features in a robust fashion, but everyone else's are absurd collections of hacks that fall to pieces at the first modification.
To an extent, 'other people's C++ / Bash scripts' have the same problem. I'm usually okay with 'other people's Java', which to me is one of the big selling points of the language - the slight wordiness and lack of 'really stupid shit' makes collaboration easier.
Now, a Python script that's more than about two pages long? That makes me question its utility. The 'duck typing' everywhere makes any code that you can't 'keep in your head' very difficult to reason about.
One thing I really dislike about Python is the double underscore thing, just really looks ugly to me and feels excessive. Just give me my flow control characters that aren't whitespace
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.
Interesting observation. Can you give an example where this is relevant?
Still better than having to create a new class just to implement
public static void main(String[] args) {}
Relevant Fireship video: https://youtu.be/m4-HM_sCvtQ
Since Java 21, this has been shortened significantly. https://www.baeldung.com/java-21-unnamed-class-instance-main
Impossible.
Only took 27 years to make the Java "Hello, world!" kinda sane.
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.
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?
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 called foo.py
, it will have the value foo
.
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.
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
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 use if__name__main__ often when working with AWS Lambda, but I also want to run it locally. Lambda wants to call a function with the params event
and context
. So I would do something like this:
def handler(event, context):
things
return {
'statusCode': 200,
'body': 'Hello from Lambda!'
}
if __name__ == '__main__':
event = {}
context = {}
response = handler(event, context)
print(response)
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.