Learning python is the point to keep in mind wrt idioms!

I have been a passionate student of the Python language for quite some time. With more than 6 years of experience working with Java [professional], combined with little experience with C ++ [hobbies], it’s fair to say that my perspective is deeply rooted in idioms generated by such statically typed, highly related languages. In short, I could say that the old-school style has a big influence on my programming style.

My reason for picking Python, not Ruby, was primarily a coincidence, since I got some of the work time, I could help using Python. it was 2 weeks, and everything was nothing more than a revolution! armed with w / IDLE and the Python Essential Reference , this was one revelation after another. it is like a classical physicist would feel if gravity ceased to exist!

In any case, I understand that for effective w / python it will take some time for real practical work. more than syntax, I feel this because of how my mind thinks. however, prepared just like me, there is one thing that bothers me quite a bit - python offers too many idioms to accomplish the same thing. For example, a list comprehension and a filter (...) apply (...) and eval (...) etc., while these idioms are not completely interchangeable, but I believe that their main goals overlap to a large extent, I understand that there should be basic performance indicators regarding their use. however, as a newbie, what's the best way to get into education and curb the distraction of the “n” ways to solve the same thing?

+4
source share
7 answers

list recognition and filter (...), apply (...) and eval (...) etc., as long as these idioms are not completely consistent, but I believe that their primary goals overlap

The pythonic paths will be: use simple for-loops or list methods. filter and map are remnants of older versions of the language. Guido wanted to be removed at some point, but it turned out that there are some valid use cases and enough people who would like them to stay (see also this thread ). Do not use eval .

Do not worry about performance if this does not become a problem (in which case the easiest way - trying to use highly optimized functions in the standard library is the best way most of the time).

I think that overall Python is really simple in trying to provide one (obvious) way to do something, although the actual (larger or smaller) options do occur, and opinions on some topics differ, of course.

Choosing Python idioms can be as simple as browsing this site, and focusing on highly supported answers to Python questions (most often there is some consensus on the best way to do something).

+4
source

Since you are running in the background of Java, I recommend reading Python is not Java . Not to mention most of the other articles in the sidebar. This article provides some good recommendations on how Java programmers can inadvertently misuse Python (and how not to do this).

+3
source
+2
source

Be sure to read the Idioms and Anti-Idioms that are part of the official Python documentation. Also be sure to read Python-style PEP8 .

+2
source

A type

 import this 

in IDLE

+2
source

Sorry if my answer doesn’t last as long as your question, but anyway, here goes:

First of all, about listcomps / filter / apply / eval. If you intend to use a filter or apply it, it is much better for you to use a list comprehension (or generator expression) or a for loop - a filter, a map, an abbreviation and an application are essentially atavisms, as far as I know and can be safely ignored. Eval has nothing to do with any of them; it just evaluates the string as Python code. You probably shouldn't use it unless you have a very good reason for this (hint: you are not doing this).

Re idioms: well, for the most part, in python there is an optimal “way to do this” for this problem, which should be used for 99% of such cases. Examples: do you need to parse / convert / generate xml? Use lxml . Do I need to create networks / almost any other type of I / O? Use twisted . Etc. Of course, there are alternatives, but more often than not there is one optimal way to do something. This is even more relevant if you just work with the standard library, as it provides many optimal solutions for common problems (although it contains some useless things).

+1
source

It's funny that you have to say this: “It was one revelation after another, it is similar to how a classical physicist would feel if gravity ceased to exist!”: Http://xkcd.com/353/

0
source

All Articles