3 ms·
python's problem is that while it goes the distance to make the simplest possible program as simple as possible, simplicity bleeds off very quickly as you add f
by awkward 11y ago
python's problem is that while it goes the distance to make the simplest possible program as simple as possible, simplicity bleeds off very quickly as you add functionality.
The shortest well behaved program, for example, looks like this:
if __name__ == "__main__":
print "Hello World!"
and the shortest program that is strictly equivalent to the java program is closer to this:
class Hello(object):
@classmethod
def main(cls):
print "Hello World!"
if __name__ == "__main__":
print "Hello World!"
Based on my experience, there's a lot people learning python who spend a lot of time at the stage where they "just don't get OO", and my opinion is that the reason is the language is bad at teaching them. It's like a submarine of complexity waiting under the simplified syntax for basic scripting.
- cosarara97 11y agoJava "teaches" its users OOP by forcing it on them. Python provides the tools to get things done without it - that's why the learners "don't get it", because they don't need it, and they don't see it as a solution to a problem (or at least this was the case when I was learning python). What you have there is un-pythonic. Once you have a good reason to build a class with static methods, it will most likely not look too bad.
- Too 11y agoWhy is this being downvoted? Classes might not be what everybody needs but this pretending that hello world can be written in 1 line is just inviting people to write sloppy code, false advertising is what it is. In my experience namespace pollution because things are written outside a function is a very common cause of bugs and maintenance problems in production python code. People coming from C/Java/similar think that "if" has scope but it doesn't, only functions do. If i see "main()-code" outside a function during a code review i immediately disregard that whole script. Any program that actually does something, except just printing hello world, would more likely look like this: import sys def main(args): function_local_variable = "yay" print("hello world") if __name__ == "__main__" pollute_global_namespace = "yes" main(sys.argv) Short of the braces and the variables i added for clarity it actually has more lines than the java-version.
- dragonwriter 11y ago> Classes might not be what everybody needs but this pretending that hello world can be written in 1 line is just inviting people to write sloppy code. IMV, sloppy unstructured code is a fine place for many (especially very young) programmers to start, and it simplifies the onramp significantly (and, actually, for simple scripts, relatively unstructured code in python is sufficient for lots of real-world uses, too.) if __name__ == "__main__" ...is the k of thing you need only if you are deliberately designing something to be used as both a script and a library, which there are very good reasons to do for many real world python files, but it certainly is not universally necessary. And using classes for hello world is excessive Java-ism.
- dragonwriter 11y agoIF you are going to use needless classes to do Java-esque hello world in Python, a shorter form that is equivalent to the Java is: class Hello(object): @staticmethod def main(): print "Hello World!" if __name__ == "__main__": Hello.main() Your version: (1) Unnecessarily uses @classmethod instead of @staticmethod, necessitating an argument to main(). @classmethod and @staticmethod in Python are both related to Java static methods, but @staticmethod is the closest analog for Java static methods that do not depend on static member data. Just doing a one-to-one substitution of Java static to Python @classmethod indicates a lack of understanding. (2) Doesn't actually call Hello.main() the way the python runtime does, and instead reproduces its function in the if __name__ == "__main__" block. > Based on my experience, there's a lot people learning python who spend a lot of time at the stage where they "just don't get OO", and my opinion is that the reason is the language is bad at teaching them. Lots of people learning Java, IME, just don't get OO for a long time, too. Obviously, they have to produce a structure with classes and methods in order to produce anything in Java, but having to use the superficial structure of OO and getting OO aren't the same thing. (If anything, being forced to use its structure for everything impairs understanding of what it is for and how to use it appropriately.)