There are hundreds of questions on StackOverflow that all ask variations of the same thing. Paraphrasing:

lst is a list of strings and numbers. I want to convert the numbers to int but leave the strings alone. How do I do that? This immediately gets a half-dozen answers that all do some equivalent of:

lst = [int(x) if x.isdigit() else x for x in lst] This has a number of problems, but they all come down to the same two:

"Numbers" is vague.
2

In Haskell, you can section infix operators. This is a simple form of partial evaluation. Using Python syntax, the following are equivalent:

(2*) lambda x: 2*x (*2) lambda x: x*2 (*) lambda x, y: x*y So, can we do the same in Python?

Grammar

The first form, (2*), is unambiguous. There is no place in Python where an operator can be legally followed by a close-paren. That works for every binary operator, including boolean and comparison operators. But the grammar is a bit tricky.
1

Many people—especially people coming from Java—think that using try/except is "inelegant", or "inefficient". Or, slightly less meaninglessly, they think that "exceptions should only be for errors, not for normal flow control".

These people are not going to be happy with Python.
2

If you look at Python tutorials and sample code, proposals for new language features, blogs like this one, talks at PyCon, etc., you'll see spam, eggs, gouda, etc. all over the place. Why?

Metasyntactic variables

If you're writing some toy code that doesn't do anything (e.g., it just demonstrates some syntax), there are obviously no meaningful names to give the variables and types in that code.

Most control structures in most most programming languages, including Python, are subordinating conjunctions, like "if", "while", and "except", although "with" is a preposition, and "for" is a preposition used strangely (although not as strangely as in C…).

But there's a whole spectrum of subordinating conjunctions that Python hasn't found a use for.

Negative subordinators

Some languages, most famously Perl, provide "unless" and "until".

There are two ways that some Python programmers overuse lambda. Doing this almost always mkes your code less readable, and for no corresponding benefit.

Don't use lambda for named functions

Some programmers don't understand that in Python, def and lambda define exactly the same kind of function object. (Especially since the equivalent is not true in C++, Ruby, and many other languages.)

The differences between def and lambda are:

def gives you a named function, lambda an anonymous function.
1

Some languages have a very strong idiomatic style—in Python, Haskell, or Swift, the same code by two different programmers is likely to look a lot more similar than in Perl, Lisp, or C++.

There's an advantage to this—and, in particular, an advantage to you sticking to those idioms. It means that whenever you do violate the idioms, that's extra meaning in your code for free. That makes your code more concise and more readable at the same time, which is always nice.
1
Blog Archive
About Me
About Me
Loading
Dynamic Views theme. Powered by Blogger. Report Abuse.