views:

991

answers:

12

I've been writing Java for the last couple of years , and now I've started to write in python (in addition).

The problem is that when I look at my Python code it looks like someone tried to hammer Java code into a python format , and it comes out crappy because - well , python ain't Java.

Any tips on how to escape this pattern of "Writing Java in Python"?

Thanks!

A: 

Learn a few other languages. It will help you make the difference between algorithms (the structure of processing, unchanged between languages) and the local syntaxic features of the language. Then you can "write Foo in Bar" for any combination of languages "Foo" and "Bar".

Thomas Pornin
A: 

This is useful if you want to understand how to code to python in a more pythonic or correct way: http://www.python.org/dev/peps/pep-0008/

WAS:

Jython? http://www.jython.org/

Or maybe you want to use a different syntax? Then use a different language.

Lex
This isn't answering the question at all... he wants to learn how to write more idiomatic (Pythonic) Python code.
TM
Then I didn't get the question. Correcting my answer.
Lex
What's the "WAS:" part for? Are you keeping your old answer around for some reason?
S.Lott
I got the -1 for the old answer, I don't know if I deserve it for the new one, that's why I keep the old one, they differ a lot in the fact that first I didn't understood the question correctly.
Lex
@Lex: "I don't know if I deserve it for the new one, that's why I keep the old one" And you'll never know as long as you keep the old wrong one around. It's wrong. Feel free to remove it and see what happens. It's not doing you any good. Why keep it around?
S.Lott
+3  A: 

You could post your code at Refactor my code to see if someone can show you a more Pythonic way to do it.

Mark Byers
+18  A: 

You might consider immersing yourself in the Python paradigms. The best way is to first know what they are then explore the best practices by reading some literature and reviewing some code samples. I recommend Learning Python by Mark Lutz; great for beginners and advanced users.

You'll do yourself a great injustice if you program with Python and fail to leverage all of the built-in, developer-friendly, Pythonic syntax.

As my French teacher used to say, "French isn't just English with different words."

Ryan Emerle
Incidentally, I got a C in French.. :)
Ryan Emerle
+1 to your teacher for the quote ;)
ercan
+1: When I first learned Python, I used semicolons to make it more Java-like and familiar. It took some time to let some of the Java-isms go. I still, however, like abstract superclasses even though they're almost useless in Python.
S.Lott
There goes my desire to learn French.
fastcodejava
Better than getting a C++ in French
jdizzle
+6  A: 

You could start by reading The Zen of Python. It'll give you some insight into how Python code is supposed to be written, provided you understand the language enough to understand what it's talking about. :-)

Kristo
+4  A: 

If you want to see some fairly idiomatic Python that does non-trivial stuff, there's Dive Into Python, although Dive Into Python 3 is newer and might be a better source of style tips. If you're looking more for some points to review, there's Code Like a Pythonista.

Hank Gay
+4  A: 

Some of the major ways in which Python differs from C/Java-like languages are:

  1. List comprehensions.

  2. Support for functional programming.

  3. The use of certain Pythonic constructs instead of similar C-like constructs although both seem to work (list comprehensions can be argued to be a part of this, but there are others).

There are others, but these are the main ones that bugged me when I first started Python (and I had come from years of Java like you).

Before using any of these, it is helpful to understand why you should go for pythonic code rather than the usual C/Java way in Python, although both give you the same output.

For starters, Python provides some powerful features not available in C/Java that makes your code much clearer and simpler (although this is subjective, and might not look any better to someone coming from Java at first). The first two points fall into this category. For example, support for functions as first class objects and closures makes it easy to do things that would need all kinds of weird acrobatics with inner classes in Java.

But a major reason is that Python is an interpreted language, and certain constructs are much faster than the equivalent C/Java-like code. For example, list comprehensions are usually a lot faster than an equivalent for-loop that iterates over the indices of a list and accesses each item by index. This is a very objective benefit, and IMHO a lot of the "Python in way too slow" way of thinking comes from using Java-style code shoe-horned into Python.

One of the best ways to learn about pythonic code is to read other people's code. I actually learnt a lot by looking at Python code posted in answers to SO questions. These often come with explanations and it is usually obvious why it is better than non-pythonic code (speed, clarity, etc.).

Edit:

Of course, there are other ways of getting other people's code. You can also download and look through the code of any good open source Python project. Books are also a good resource, I would recommend O'Reilly Python Cookbook. It has lots of useful code examples and very detailed explanations.

MAK
Duck Typing should be in the top 3 list of differences, if not #1
Graphics Noob
@Graphics Noob: Duck typing should be in the top 3, but this was not a list of the top 3. I just mentioned a few from the top 5 :). My point is that these are some that people don't get at first, and so avoid and end up writing Java in Python. Duck typing, while being a profound difference, is something that is somewhat easier to appreciate IMO, compared to something like closures and list comprehensions.
MAK
+2  A: 

Definitely not a panacea but I think you should try some code golf in Python. Obviously nobody should write "golfed" code IRL, but finding the most terse way to express something really forces you to exploit the built in functionality of the language.

Graphics Noob
+1, good advice
MAK
What does "golfed" code means?
yossale
+4  A: 

If you are new to Python and coming from Java (or C#, or other similar statically typed OO language), these classic articles from PJ Eby and Ryan Tomayko are necessary reading:

Corey Goldberg
+1  A: 

Someone provided me with this list of how "Python is not Java" when I started Python after Java, and it was very helpful.

Also, check out this similar SO question that I posted a short time ago when in a similar position.

froadie
Thanks! I checking it :)
yossale
A: 

Eat Python, Sleep Python and Drink Python. That is the only way........

A: 

Try to find algorithms that you understand well and see how they are implemented in python standard libraries.

Persist. :)

Unreason