Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

learning python - point to keep in mind w.r.t idioms!

Tags:

python

syntax

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?

like image 816
anirvan Avatar asked Dec 09 '22 13:12

anirvan


1 Answers

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).

like image 191
ChristopheD Avatar answered Dec 21 '22 10:12

ChristopheD