Besides the dynamic nature of Python (and the syntax), what are some of the major features of the Python language that Java doesn't have, and vice versa?
6 Answers
List comprehensions. I often find myself filtering/mapping lists, and being able to say
[line.replace("spam","eggs") for line in open("somefile.txt") if line.startswith("nee")]is really nice.Functions are first class objects. They can be passed as parameters to other functions, defined inside other function, and have lexical scope. This makes it really easy to say things like
people.sort(key=lambda p: p.age)and thus sort a bunch of people on their age without having to define a custom comparator class or something equally verbose.Everything is an object. Java has basic types which aren't objects, which is why many classes in the standard library define 9 different versions of functions (for boolean, byte, char, double, float, int, long, Object, short).
Array.sortis a good example. Autoboxing helps, although it makes things awkward when something turns out to be null.Properties. Python lets you create classes with read-only fields, lazily-generated fields, as well as fields which are checked upon assignment to make sure they're never 0 or null or whatever you want to guard against, etc.'
Default and keyword arguments. In Java if you want a constructor that can take up to 5 optional arguments, you must define 6 different versions of that constructor. And there's no way at all to say
Student(name="Eli", age=25)Functions can only return 1 thing. In Python you have tuple assignment, so you can say
spam, eggs = nee()but in Java you'd need to either resort to mutable out parameters or have a custom class with 2 fields and then have two additional lines of code to extract those fields.Built-in syntax for lists and dictionaries.
Operator Overloading.
Generally better designed libraries. For example, to parse an XML document in Java, you say
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse("test.xml");
and in Python you say
doc = parse("test.xml")
Anyway, I could go on and on with further examples, but Python is just overall a much more flexible and expressive language. It's also dynamically typed, which I really like, but which comes with some disadvantages.
Java has much better performance than Python and has way better tool support. Sometimes those things matter a lot and Java is the better language than Python for a task; I continue to use Java for some new projects despite liking Python a lot more. But as a language I think Python is superior for most things I find myself needing to accomplish.
5 Comments
Collections.sort(people, (a b) -> Integer.compare(a.getAge(), b.getAge())), though I really wish the standard library supported Collections.sortByKey(people, Person::getAge)).I think this pair of articles by Philip J. Eby does a great job discussing the differences between the two languages (mostly about philosophy/mentality rather than specific language features).
Comments
One key difference in Python is significant whitespace. This puts a lot of people off - me too for a long time - but once you get going it seems natural and makes much more sense than ;s everywhere.
From a personal perspective, Python has the following benefits over Java:
- No Checked Exceptions
- Optional Arguments
- Much less boilerplate and less verbose generally
Other than those, this page on the Python Wiki is a good place to look with lots of links to interesting articles.
4 Comments
With Jython you can have both. It's only at Python 2.2, but still very useful if you need an embedded interpreter that has access to the Java runtime.
Comments
Apart from what Eli Courtwright said:
- I find iterators in Python more concise. You can use for i in something, and it works with pretty much everything. Yeah, Java has gotten better since 1.5, but for example you can iterate through a string in python with this same construct.
- Introspection: In python you can get at runtime information about an object or a module about its symbols, methods, or even its docstrings. You can also instantiate them dynamically. Java has some of this, but usually in Java it takes half a page of code to get an instance of a class, whereas in Python it is about 3 lines. And as far as I know the docstrings thing is not available in Java
1 Comment
Java is a Stable Language for many years.
- There is nothing that Java Do not have and any other language have.
- If you think Java doesn't have this, means you don't know.
- And if Java doesn't have something it must be a reason. For example Java doesn't have pointers doesn't mean it is bad than C++, It has a reason, you do not need it in Java.
- First understand how JDK, JRE, J2ME, Java Card, JavaFX and JVM works (for the starters). Java is not just a syntax that you can compare.
- No matter what anyone say WORA, WORE, Security, In-bult features of JVM, combination of being Compiled, Interpreted, and JITC these are unmatched to any programming language.
- In java now, if you know how to work you do not have to even write the code much there are tools for everything. (Where in Python or any language you have to write the code, you cannot just generate pre written logic like in java. So Java might have 6000 line but among them only 50 lines were manually written but in Python you have to write all the 6000 line, I personally wont like it all, Why I am unnecessary writing the code to save/update the data with 500000 columns in Database every time, if my code can do it on its own in 10 lines using generating pre-written logic?)
I will answer all the other question as it will come as an comments.