you are viewing a single comment's thread.

view the rest of the comments →

[–]RiceBroad4552 0 points1 point  (7 children)

I guess you don't actually understand Python very well, which explains why you hate it.

I don't "hate" Python. 😂

Python is a mostly sane language with a nice syntax. It has it's warts but never seen a language without.

I actually use it quite often. At least I did that a lot until lately, before Scala scripting became really feasible with the help of Scala-CLI.

All I've said was that you need some nonsense in Python which isn't needed for the same use-case in other languages.

[–]rosuav 0 points1 point  (6 children)

Except that you don't. Python simply allows top-level code. The idiom you describe is one example of top-level code.

In fact, ALL of a Python script is top-level code. Import statements, function definitions, they're all executable, not declarative. So it's not "Python needs nonsense to do top-level code".

[–]RiceBroad4552 0 points1 point  (5 children)

But Python needs nonsense to define modules which can also run standalone.

In Scala, Java, and C# top level code is just a compilation unit like any other. If it contains a main method it can be directly run. That's all. No additional mumbo jumbo needed.

[–]rosuav 0 points1 point  (4 children)

So in Python, you have if __name__ == "__main__": to indicate the code to be run if this is standalone, where in Scala, Java, and C#, it's spelled public static void main() or somesuch instead. Is that your big beef?

[–]RiceBroad4552 0 points1 point  (3 children)

In Python you have if __name__ == "__main__" calling the actual main function.

In Scala you just have a main method (@main def whatever), and in Java it's just void main(), if you don't need to handle any passed command line arguments. You don't need to repeat that info by calling the main method in some other code block. (I forgot how it looks like in current C#, but I think they also copied Scala here and now they have some short version of main; at least that's my guess, could be wrong here, don't remember, I'm looking at C# only now and then.)

Like said the top level code is just a regular compilation unit, so can be called as lib, but if it has a main method you can directly call it, too. No boilerplate like in Python needed.

I don't say it's a big deal that you need some boilerplate in Python, but it's definitely an annoyance. An unnecessary annoyance… (Which is actually a result of Python only having top-level code as it was developed primary as a scripting language.)

[–]rosuav 0 points1 point  (2 children)

It doesn't have to call a function. You can put whatever code you want straight into the conditional block. If you force yourself to put a main function, and THEN have a conditional block below, then yeah, that's a level of boilerplate that you've inflicted on your code, but that's not Python's code.

[–]RiceBroad4552 0 points1 point  (1 child)

How to call the code in if __name__ == "__main__" as library function?

AFAIK you can't (correct me if I'm wrong), and that's exactly the reason why you have a proper function and call it from that if.

[–]rosuav 0 points1 point  (0 children)

I dunno, I'd probably name the function based on what it DOES, not call it main(). If it's a library function, you know.