views:

246

answers:

6

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?

+8  A: 

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.

cletus
I second Code Like a Pythonista.
loliii
And "Python is Not Java". http://dirtsimple.org/2004/12/python-is-not-java.html
Ignacio Vazquez-Abrams
+3  A: 

The best place to start is probably PEP-8, which is the official Python style guide. It covers a lot of the basics for what is considered standard.

Amber
Is it just me, or has PEP-8 been referenced quite a bit more frequently in these last few days than it had been?
dash-tom-bang
It's been frequently referenced for quite a while as far as I've seen.
Amber
A: 

"Everything is a class" is a Java idiom that's specifically not a Python idiom. (Almost) everything can be a class in Python, and if that's more comfortable for you then go for it, but Python doesn't require such a thing. Python is not a purely object-oriented language, and in my (limited) experience it's good to take that to heart.

Peter Milley
+4  A: 

Jacob Hallén once observed that the best Python style follows Tufte's rejection of decoration (though Tufte's field is not programming languages, but visual display of information): don't waste "ink" (pixels) or "paper" (space) for mere decoration.

A lot follows from this principle: no redundant parentheses, no semicolons, no silly "ascii boxes" in comments and docstrings, no wasted space to "align" things on different rows, single quotes unless you specifically need double quotes, no \ to continue lines except when mandatory, no comments that merely remind the reader of the language's rules (if the reader does not know the language you're in trouble anyway;-), and so forth.

I should point out that some of these consequences of the "Tufte spirit of Python" are more controversial than others, within the Python community. But the language sure respects "Tufte's Spirit" pretty well...

Moving to "more controversial" (but sanctioned by the Zen of Python -- import this at an interpreter prompt): "flat is better than nested", so "get out as soon as sensible" rather than nesting. Let me explain:

if foo:
  return bar
else:
  baz = fie(fum)
  return baz + blab

this isn't terrible, but neither is it optimal: since "return" ``gets out'', you can save the nesting:

if foo:
  return bar
baz = fie(fum)
return baz + blab

A sharper example:

for item in container:
  if interesting(item):
    dothis(item)
    dothat(item)
    theother(item)

that large block being double-nested is not neat... consider the flatter style:

for item in container:
  if not interesting(item):
    continue
  dothis(item)
  dothat(item)
  theother(item)

BTW, and an aside that's not specifically of Python-exclusive style -- one of my pet peeves (in any language, but in Python Tufte's Spirit supports me;-):

if not something:
  this()
  that()
  theother()
else:
  blih()
  bluh()
  blah()

"if not ... else" is contorted! Swap the two halves and lose the not:

if something:
  blih()
  bluh()
  blah()
else:
  this()
  that()
  theother()
Alex Martelli
Figuring out whether to `return` a value halfway through a function or use an `else` statement often leaves me paralysed with indecision...
detly
A: 

Syntax is only the tip of an iceberg. There are a number of different language construct that Java programmers should be aware of, e.g. Python do not need to use interface

Creating an interface and swappable implementations in python - Stack Overflow

The other really useful idiom is everything can be convert to a boolean value with an intuitive meaning in Python. For example, to check for an empty array, you simply do

if not my_array:
  return
...process my_array...

The first condition is equivalent to Java's

if ((my_array == null) || (my_array.length == 0)) {
  return
}

This is a godsend in Python. Not only is it more concise, it also avoid a Java pitfall where many people do not check for both conditions consistently. Countless NullPointerException are averted as a result.

Wai Yip Tung