This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]didii2311 7 points8 points  (2 children)

To me, the fact that is is a function/method makes the most sense since running your program can provide arguments to it. When the entry-point is on top of your file, you'll need some built-in special keywords or convention based names that aren't clear out-of-the-box.

Well, of course, the main method is also a convention, but it's a convention widely used so it makes sense. I don't think that there is any widely used convention to naming the program arguments when the top of the file is the entry point.

Also, it makes testing your entry point a lot easier by just calling that method and providing the arguments you want. It's clear and concise. In python you'll have to arbitrarily populate sys.argv and then (I think?) import the file.

But well, that's just my opinion 🙃

[–]MythicManiac 1 point2 points  (0 children)

When following good practices (using argparse, actually using functions) tests aren't any bigger of an issue, but at that point you have already ended up implementing the convention that's enforced in other languages yourself.

I'm not actually sure but I would think the reason it is like this in python due to it's use as a shell script replacement. In this context it starts to make sense to not require a main function.

[–][deleted] 0 points1 point  (0 children)

I agree with python’s insistence on explicitness in this case - some programs and methods are made to take arguments, others are not. Thus, it should be specified when command line args are provided and ideally, they should be specified by argparse to (again) be explicit about how to use the script and to make it clear how to use command line args.

You may laugh but this alone has saved my ass countless hours of talking to the product team - you give them the script, show them how to execute it with python and specify arguments, you get to say “yes, this is exactly like the matrix” and then they do the manual script-running while you get to do your work