views:

151

answers:

7

I've been an avid learner of the Python language for quite some time. Having more than 6 years of Java[professional] experience, coupled with a bit of C++ [hobby] experience - it's fair to say my perspective is deeply entrenched in the idioms brought forth by such statically typed, strongly bound languages. In short - i could say the old school way of thinking has significant influence on my programming style.

My reason to pick up Python, and not say Ruby, was primarily a coincidence since i got some part time work i could help out w/ using Python. it's been 2 weeks, and things have been nothing short of a revolution! armed w/ IDLE and the Python Essential Reference, it's been one revelation after another. it's like how a classical physicist would feel if gravity ceased to exist!

Anyways, i understand that to be effective w/ python it's going to take some time of real hands-on work. more than the syntax, i feel it's because of the way my mind thinks. however, prepared as much as i am, there's one particular thing which bothers me quite a bit - python offers way too many idioms to perform the same thing. For example, list comprehension and filter(...), apply(...) and eval(...), etc. while these idioms aren't completely substitutable, but i find that their primary purposes overlap to a great extent. i understand that there must be underlying performance gains vis-a-vis their usages. however, as a beginner, what's the best way to get on w/ the education and curb the distraction of 'n' ways to solve the same thing?

+2  A: 

For starters, you should read this:

http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html

FogleBird
+1  A: 

Sorry, if my answer is not as long as your question, but, anyway, here goes:

First of all about listcomps/filter/apply/eval. If you are about to use filter or apply you are much better off using a list comprehension (or a generator expression) or a for loop, - filter, map, reduce and apply are, basically, atavisms as far as I know and can be ignored safely. Eval does not have anything to do with either of those, it just evaluates a string as python code. You probably should not use it, unless you have an extremely good reason to od so (hint: you don't).

Re idioms: well, for the most part, in python for a given problem there is an optimal 'way to do it', that should be used for 99% of similar cases. Examples: need to parse/transform/generate xml? Use lxml. Need to do networking/pretty much any other kind of i/o ? Use twisted. And so on. Of course there are alternatives, but most of the time there is indeed one optimal way of doing things. This is even more relevant if you are just working with the standard library, as it provides lots of optimal solutions to common problems (although it does contain some rather useless stuff).

shylent
+4  A: 

list comprehension and filter(...), apply(...) and eval(...), etc. while these idioms aren't completely substitutable, but i find that their primary purposes overlap to a great extent

The pythonic way would be: use simple for-loops or list comprehensions. filter and map are remnants of older versions of the language. Guido wanted them removed at one point but it turned out there are some valid use cases and enough people who would like them to stay (also see this thread). Don't use eval.

Don't worry about performance unless it becomes a problem (and in that case the easiest way - trying make to use of the highly optimized functions in the standard library - is the best way most of the time).

I think in general Python is really straightforward in trying to provide one (obvious) way to do things, although valid (bigger or smaller) variations do occur and opinions on some topics do differ, of course.

Picking up the Python idioms can be as easy as browsing this site and paying special attention to highly upvoted answers on Python questions (most of the time there's some kind of consensus on the best way to do things).

ChristopheD
A: 

Funny you should say this: "it's been one revelation after another. it's like how a classical physicist would feel if gravity ceased to exist!": http://xkcd.com/353/

Ned Batchelder
Not downvoting, but would have been more appropriate as a comment imho...
ChristopheD
yeah - but a coincidence indeed! :]
anirvan
I find it amusing that the hello-world in that comic is no longer valid Python, but still valid Ruby.
Ken
isn't it? last i checked, that's just how it seems to work for Py too.
anirvan
@Ken means that it isn't valid in Python 3.x. It's still right in 2.x.
Ned Batchelder
+2  A: 

Be sure to read Idioms and Anti-idioms which is part of the official Python documentation. Also be sure to read PEP8 on Python style.

limist
+2  A: 

Type

import this

into IDLE

bigjim
+3  A: 

Since you're coming from a Java background, I recommend reading Python is not Java. Not to mention most of the other articles in the sidebar. The article gives some good pointers on how Java programmers can unintentionally mis-use Python (and how not to).

John C
Nice article. EVERY ONE of these pieces of advice also applies to Java. The tricks still work, they just take a little more planning to implement in Java than they do in Python, but they are all possible and all will improve your code (with the exception of putting data into your code--I think DSLs implemented in such a way as to allow your language to compile them leads to horrific and stupid syntax, just create a REAL dsl and parse it yoursef, dammit. Data should be extracted from code completely, not just given a stupid syntax!
Bill K
thanks for the article. really sets the tone of what to expect, and more importantly, how to digest things w.r.t practical usage.
anirvan