This page follows a [http://groups.google.ca/groups?hl=en&lr=&selm=mailman.8378.1103832808.5135.python-list%40python.org&prev=/groups%3Fq%3Ddubious%2BFernando%26hl%3Den%26lr%3D%26group%3Dcomp.lang.python.*%26selm%3Dmailman.8378.1103832808.5135.python-list%2540python.org%26rnum%3D2 suggestion] of FernadoPerez on comp.lang.python. His suggestion was that there should somewhere be a collection of bad python practices together with an explanation of the badness and a preferred alternative. This seems a good job for a wiki-page, so ...
String concatenation is building up a relatively lengthy string from a collection of strings.
Newcomers to Python often try to build strings up like this:
This is slow and resource heavy. Each time through the for loop, a new string is built and the old one is discarded. That might not matter so much for such a small case, but as the number of elements to be joined creeps up, so too does the inefficiency.
The above code is perfectly fine. Its readible, and it works. Worrying about speed/performance when it is not an issue is one of the worst programming practices. "Premature Optimization is the Root of All Evil" -- see PrematureOptimization
For short cases where the number of strings to be joined is known, you can use string formatting as follows:
This is much more efficient, but is also rather more limited in the range of circumstances to which it applies. It could be made more general by constructing the formatting string as a function of len(string_list), but this would be a bit dubious, too. It's also less readible, less maintainible. A better alternative is found in the next section.
The join method of strings
The join method of the string type lets you perform the concatenation as follows:
This is quite efficient and perfectly general as it applies to any arbitrary list of strings. (You don't need to know the list length in advance.)
The major thing to puzzle the newcomer here is why "".join(some_list) rather than some_list.join(). The way to think of this is that you are using the string "" to join the elements of some_list. Hence,
That said, some do consider this aspect of the join method of strings odd enough to count as a PythonWart.
If the ''.join(some_list) syntax really bothers you, one option is to bind the method to a different name, e.g.
The audience should be an expert in the idioms of a language when considering readability. The join is simple to this crowd.
Overly Verbose Conditionals
Among the most common tasks in programming is to test if a condition obtains and act accordingly. It is common for newcomers to Python to adopt an all-together overly verbose idiom for this.
There is a slight speed of execution inefficiency in these examples. The first example has the overhead of an extra method lookup (bool.__eq__) and an extra name lookup (True). The second example has the overhead of an extra branch statement. The third example has the overhead of two extra method lookups (somecontainer.__len__ and int.__cmp__).
But much more important is the speed of entry and understanding inefficiency. All other things being equal, extra typing is evil. And, unless some substantial gain in clarity is purchased by the extra characters, the more characters in the code, the longer that code will take to understand. (The programming time you save could well be your own!)
Most non-empty containers evaluate to True in a boolean context, so no test on len() is generally necessary:
Some containers (e.g. numarray.array) do not evaluate this way. In these cases, the preferred idiom is:
Overuse of lambda
Lambda forms allow anonymous functions to be created and used as part of an expression. However, when a function is already named, wrapping this function in a lambda can decrease readability and affect program efficiency.
Using a lambda when a function is already named incurs the extra overhead of one function call, which is generally undesirable as function calls are relatively expensive in Python. Even setting execution efficiency aside, the lambda-less versions are preferred because they are generally more concise and easier to read.
Counting Items without Enumerate
It is often useful to keep track of the index of an item in an iterable, for example, for reporting the line number of a string in a file. As of Python 2.3 the preferred way to do this is using the builtin enumerate instead of a manually updated count variable. In versions of Python before 2.3, this is actually not all that dubious...
While timings are comparable, manual update is more verbose and has a greater risk of programming error if the programmer forgets to update the count variable.
Overuse of Regular Expressions
Regular Expressions provde a powerful tool for doing complicated string searches. However, for simple string searches, regular expressions are often overkill.
(Note that such overuse of regular expressions is often the result of converting Perl code to Python code.)
For simple tasks, using a regular expression can add unnecessary overhead from compiling the regular expression and using the match object to search. When applicable, using string methods can often be faster and more concise.
Note that in pre-2.3 Pythons 'in' only worked with single character strings. In Python 2.3 and above, 'in' works with multi-character substrings as above.
1 words = s.split()
Note that this is at the bottom not because it is less significant than any of the other problems but because, unlike the above idioms, is not a problem specific to Python, but a general programming problem.
Premature Optimization is spending effort on execution efficiency before determining which parts of the code are actually significant to the program efficiency.
This occurs in a variety of contexts, all of which involve spending extra time making code run faster before first writing a simple, concise implementation that produces the correct results.
Example of over-optimizing attribute accesses:
While a correctly applied optimization can indeed speed up code, optimizing code that is only seldom used can waste significant development time, and can make code harder to read. Optimizations should only be sought when a programmer has isolated (using a profiler, etc.) a significant bottleneck in program efficiency. Write correct code first, then make it fast (if necessary).
Don't optimize until necessary.
Example of non-optimized but more readable attribute access:
The above examles look equally readable to me. In fact, the binding of a function to an object enhances readiblity and maintainability if used more than once: is preferred over since the binding of lst and append happens twice - copy and paste.
app = lst.append
enhances readiblity and maintainability if used more than once:
is preferred over
since the binding of lst and append happens twice - copy and paste.