Recently, there have been a few proposals to change Python's syntax to make it easier to avoid break and continue statements.

The reasoning seems to be that many people are taught never to use break and continue, or to only have a single break in any loop. Some of these people are in fact forced to follow these rules for class assignments.

These rules are ludicrous. The stdlib has hundreds of break and continue statements. There are dozens of them in the official tutorial, the library reference docs, Guido's blogs, etc. The reasons for avoiding break and continue in other languages don't apply to Python, and many of the ways people use to avoid them don't even exist in Python. Using break and continue appropriately is clearly Pythonic.
1

Sometimes, you need to split a program into two parts.

For many cases--one step in your process briefly uses a ton of memory, and you want that to be released to the system, or you want to parallelize part of the process to take advantage of multiple cores--you just use multiprocessing or concurrent.futures.

But sometimes that doesn't work. Maybe the reason you want to split your code is that you need Python 3.3 features for one part, but a 2.7-only module for another part.

Look at this familiar code:

class Foo(object): def __init__(self, a): self.a = a def bar(self, b): return self.a + b foo = Foo(1)

How do __init__ and bar get that self parameter?

Unbound methods

Well, bar is just a plain old function. (I'll just talk about bar for simplicity, but everything is also true for __init__, except for one minor detail I'll get to at the end.)

Foo.bar is a method, but it's an "unbound method"—that is, it's not bound to any particular Foo instance yet.
3

Never call readlines() on a file

Calling readlines() makes your code slower, less explicit, less concise, for absolutely no benefit.

There are hundreds of questions on places like StackOverflow about the readlines method, and in every case, the answer is the same.

"My code is takes forever before it even gets started, but it's pretty fast once it gets going." That's because you're calling readlines.
6

You don't actually have to be a dummy to not get list comprehensions. Only a few languages (after Python, the next most popular is probably Haskell) support them. And they're only "easy" once you learn to think a different way.

You do eventually want to learn to think that way.
Blog Archive
Loading
Dynamic Views theme. Powered by Blogger. Report Abuse.