Java -> Python?
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?
Asked by: Clark465 | Posted: 28-01-2022
Answer 1
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.sort
is 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.
Answered by: Kelsey475 | Posted: 01-03-2022Answer 2
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).
Answered by: Brad753 | Posted: 01-03-2022Answer 3
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.
Answered by: Tara625 | Posted: 01-03-2022Answer 4
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.
Answered by: John166 | Posted: 01-03-2022Answer 5
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
Similar questions
Java to Python RSA
I'm trying to encrypt a string from Java to Python, using the library Bouncy Castle J2ME on the client side and Python M2Crypto on the other.
Everything is pretty good, I can decrypt it properly, but the padding is the issue.
The M2Crypto lib gives me (as far as I can tell) only these Padding schemes:
no_padding = 3
pkcs1_padding = 1
sslv23_padding = 2
pkcs1_oaep_padding = 4
While the bouncy castle ...
How Similar are Java, C#, and Python?
Python in java, is it possible
I have a class that is written in Java.
Can it be used in Python so i dont have to rewrite it?
java - How can I specify an AES key in Python?
I'm working on converting a Java program into Python and part of its core networking uses AES encryption to handle packets going up and down the line. Java's AES is initialized like so:
byte[] key = { 0x13, 0x00, 0x00, 0x00 };
sKeySpec = new SecretKeySpec(key, "AES");
I want to do the same in Python, and will use PyCrypto, but I'm not sure how to initialize the above in it as it only allow...
Java and Python
I am trying to open a java link I have saved on my folder on my desktop. I can't get it to find the file. The java address is x:\green\Adam\FILEJAR.jar. This is an executable JAR file I want to open it and have it on my desktop, just open it and have it run as normal for the Java link. Want Python just to open it.
import subprocess
run="x:\green\Adam\FILEJAR.jar"
proc=subprocess.Popen(run)
Python with java
Instead of using Jython, is there any way to call a Java program from Python?
This Java program contains some function and I need to give an input file to Java code and it returns two values which I will be using in Python. So the Python program passes a filename to the Java code and the Java code will return two values for each line in that file.
For example I have the following file containg data like gi...
Python GUI from Java
I am working on a program which takes a user input and generates an output as a map projection plot.
The easiest map projection library that I have found is matplotlib-basemap, written in python a language I am not much familier with (I work on Java ).I have written the user interface in Java. Currently I am executing the python code and sending command arrays with data using Runtime and exec() command calling the ".py" f...
java - Read AVRO file using Python
I have an AVRO file(created by JAVA) and seems like it is some kind of zipped file for hadoop/mapreduce, i want to 'unzip' (deserialize) it to a flat file. Per record per row.
I learned that there is an AVRO package for python, and I installed it correctly. And run the example to read the AVRO file. However, it came up with ...
python - What's the best Django search app?
How can I use a DLL file from Python?
What is the easiest way to use a DLL file from within Python?
Specifically, how can this be done without writing any additional wrapper C++ code to expose the functionality to Python?
Native Python functionality is strongly preferred over using a third-party library.
python - PubSub lib for c#
Is there a c# library which provides similar functionality to the Python PubSub library? I think it's kind of an Observer Pattern which allows me to subscribe for messages of a given topic instead of using events.
python - What is the best way to copy a list?
This question already has answers here:
python - Possible Google Riddle?
My friend was given this free google website optimizer tshirt and came to me to try and figure out what the front logo meant.
t-shirt
So, I have a couple of guesses as to what it means, but I was just wondering if there is something more.
My first guess is that eac...
How do you check whether a python method is bound or not?
Given a reference to a method, is there a way to check whether the method is bound to an object or not? Can you also access the instance that it's bound to?
ssh - How to scp in Python?
What's the most pythonic way to scp a file in Python? The only route I'm aware of is
os.system('scp "%s" "%s:%s"' % (localfile, remotehost, remotefile) )
which is a hack, and which doesn't work outside Linux-like systems, and which needs help from the Pexpect module to avoid password prompts unless you already have passwordless SSH set up to the remote host.
I'm aware of Twisted'...
python - How do I create a new signal in pygtk
I've created a python object, but I want to send signals on it. I made it inherit from gobject.GObject, but there doesn't seem to be any way to create a new signal on my object.
python - What do I need to import to gain access to my models?
I'd like to run a script to populate my database. I'd like to access it through the Django database API.
The only problem is that I don't know what I would need to import to gain access to this.
How can this be achieved?
python - How do I edit and delete data in Django?
I am using django 1.0 and I have created my models using the example in the Django book. I am able to perform the basic function of adding data; now I need a way of retrieving that data, loading it into a form (change_form?! or something), EDIT it and save it back to the DB. Secondly how do I DELETE the data that's in the DB? i.e. search, select and then delete!
Please show me an example of the code ...
Still can't find your answer? Check out these communities...
PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python