Still useful for running 2- or 3-line dummy scripts directly from the command line. For example you just installed a new package and want to validate it's working,
Or for dirty .pth tricks where you're obliged to start with an import statement and have everything on a single line. But 3.15 seems to be moving away from those to some new-fangled .start files.
The problem with that is not only would it break old code, but the neat little trick of JSON being directly embeddable would break. And who would want that.
Edit: I forgot that “true”, “false”, and “null” don’t exist in Python. Not the hardest thing to fix by declaring some local variables, but at that point you’re doing too much and should use a library.
Edit 2: added quotes around the keywords to disambiguate them from the general concepts
JSON (or any data format) being directly embeddable sounds nice in theory, but isn't really a great idea in practice.
JSON was originally intended to be loaded via eval. This was exploited almost immediately, which is why JS got proper JSON parsing and serialization functions. You should always run JSON (and any other serialized format) through a proper parser to load it. If you're clever, you can do this at build time, but you should never rely on any data file being valid at runtime.
Well yes. I know. I think of Python’s syntax being mostly compatible with JSON as a
> neat little trick.
I actually forgot that eval() was a thing when I wrote that comment.
What I meant was, you could copy and paste your own JSON into your source code for a simple payload in a throwaway script, for whatever you may need it for.
Edit: Reddit mobile app doesn’t want to make that a block quote. You get the idea though.
hate to be a party pooper but the meme refers specifically to indentation-based structure vs explicit code block delimiters
oh and btw the latter are vastly superior because they make for a free-form syntax that, unlike python, can be automatically formatted by a relatively simple program/plugin (i.e. without AI)
there's main() in python ONLY IF YOU WANT THERE TO BE.
the way you want it to be is as follows:
import foo
from baz import bar
do_stupid_stuff()
def main():
print("I am not kidding")
if ___name___ == '___main___':
main()
if ____name____ wasn't ____main___ then your script knows oh I'm not __main__, uh might as well uh evaluate the true path of this if clause I guess, else whatever. This is very robust and safe way to define the entrypoint.
1.0k
u/belabacsijolvan 2d ago
theres main() in python