Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python readability hints for a Java programmer

I'm a java programmer, but now entering the "realm of python" for some stuff for which Python works better. I'm quite sure a good portion of my code would look weird for a Python programmer (e.g. using parenthesis on every if).

I know each language has its own conventions and set of "habits". So, from a readability standpoint what are conventions and practices which is "the way to go" in Java, but are not really the "pythonic way" to do stuff?

like image 862
Samuel Carrijo Avatar asked May 20 '10 00:05

Samuel Carrijo


People also ask

Can a Java programmer learn Python easily?

It's got incredibly concise syntax and is super easy to learn. Python code often looks like a pseudo-code; you tend to focus more on writing your logic and less on the intricacies of the language. That's one of the main reasons for its popularity, especially in the world of data science.

Should a Java developer learn Python?

Python has an extremely simple syntax compared to most of the other programming languages. With Python, you do not even need brackets to define a code block. A simple indentation will do the job. Owing to its simplicity, Python is one of the easiest languages to learn.

What helps with readability in Java code?

Predicates not only improves readability but also enhances reusability. If you find yourself repeating the same "condition" in multiple places in your code, consider extracting it out as a Predicate and giving it sensible name. By doing so, you also documented your condition. if( user.

How hard is it to learn Python after Java?

It is easier to make the transition from Java to Python or any other language, but the reverse is a bit difficult because of Python syntax is a bit different than most other popular programming languages.


1 Answers

There's no simple answer to that question. It takes time for your code to be "Pythonic". Don't try and recreate Java idioms in Python. It will just take time to learn Python idioms.

Take a look at Code Like a Pythonista: Idiomatic Python, Style Guide for Python Code and Python for Java Programmers (archived).

like image 51
cletus Avatar answered Oct 03 '22 13:10

cletus