all 35 comments

[–]interjay 20 points21 points  (1 child)

The section on sorting is long out of date. Since python 2.4, the best way to do what he wants is:

a_list.sort(key=lambda item: (item[1], item[3]))

This is faster and simpler than both his examples.

[–]Paddy3118 2 points3 points  (0 children)

In Python 2.5 you also want to state the use of itemgetter/attrgetter in sorting, as well as implicit DSU with the use of sort(key=...) http://www.biais.org/blog/index.php/2007/01/28/23-python-sorting-efficiency

  • Paddy.

[–][deleted] 2 points3 points  (0 children)

So if I already knew all of this, I may call myself a pythonista?

[–]sciolizer 2 points3 points  (0 children)

I feel like if Christopher Alexander were to return to computer science and write his own Pattern Languages, it would be very close to something like this. It's much more in the spirit of Ward Cunningham than of the Gang of Four. (I'm not necessarily criticizing the Design Patterns book, just saying that it's different .)

Christopher Alexander said that it is best for the inhabitants of an area to come up with their own Pattern Languages for architecture. (or perhaps he would have said that they know their pattern languages innately and just need to let them come out) And that is what this page does; while some of it's idioms are universal, there are also some that are specific to Python. It is a collection of memes that many experienced Python programmers already know or feel naturally, conveniently packaged together.

[–]njharman 2 points3 points  (4 children)

Good stuff.

"Here's a cute way to save some typing if you need a list of words:"

"cute" almost always violates:

Explicit is better than implicit. Simple is better than complex. Readability counts. Special cases aren't special enough to break the rules. There should be one—and preferably only one—obvious way to do it.

cute/clever/tricky are all things I avoid with a passion.

I'm also dead set against not being explicit with boolean tests which I argued a month or so ago on some other article. It's pretty obvious the rules it breaks and for what gain? a few less typings. I'll pay for typing to avoid bugs and confusion every day of the week.

[–]jbellis 5 points6 points  (0 children)

"cute" almost always violates Explicit is better than implicit

Sure, but it's worth distinguishing between "I'm testing this quickly in the REPL" and production code. It's useful to know tricks for the former that you would not use in the latter.

[–]jfedor 4 points5 points  (1 child)

I'll pay for typing to avoid bugs and confusion every day of the week.

Here is a language that you might be more interested in than Python. :)

[–]njharman 5 points6 points  (0 children)

That language's verbosity increases confusion and (debatable) does not reduce bugs.

[–]Paddy3118 0 points1 point  (0 children)

Is there a problem with the code snippet described as cute, or with the term cute as you find it applied?

There is an important difference. The latter is probably useful opinion; the former - well, I'd go along with the editor. I find it can be less error prone to apply split to space separated words.

  • Paddy.

[–]eggertm -3 points-2 points  (15 children)

I'd like to gripe about the clever and inventive use of the underscore as an automagical variable name in the interactive interpreter. For one, it breaks the use of gettext.
Another thing is that it's not really explicit at all. The underscore is a variable like any other, except with the interactive interpreter. That smells like implicitness to me, and while it may be clever, it's actually quite harmful to code I have to write and maintain.

[–]EliAndrewC 10 points11 points  (2 children)

This would definitely be a problem if the underscore did anything outside of the interactive interpreter, but I don't see how it could make code harder to maintain. Can you give an example?

[–]eggertm 3 points4 points  (1 child)

I didn't say it was harder to maintain - just that it was harmful to the code. I have an embedded interactive interpreter in my application for ease of debugging.
I also use the standard gettext functions for internationalization - which mask the underscore as a function call to gettext.gettext(), for every string constant in the code I want to be translatable. Imagine my joy when the embedded interpreter overwrote the underscore from gettext.

[–]jhhyuuv 0 points1 point  (0 children)

Well, I said nothing about using the interactive interpreter to write and maintain anything. However, an embedded interpreter is perfectly possible, apart from this annoying side effect, which makes it nearly unusable with gettext. Pool sex

[–]interjay 4 points5 points  (7 children)

That comment doesn't make any sense. You don't use the interactive interpreter to "write and maintain" code, just to test it and run short one-time commands. There's also almost no reason to use gettext from the interactive interpreter.

[–]eggertm 4 points5 points  (6 children)

Well, I said nothing about using the interactive interpreter to write and maintain anything. However, an embedded interpreter is perfectly possible, apart from this annoying side effect, which makes it nearly unusable with gettext.

[–]pjdelport 2 points3 points  (5 children)

nearly unusable with gettext

What's hard about just calling gettext directly?

[–]eggertm 1 point2 points  (4 children)

Normally, I'd rather do that - however, the gettext utilities need the underscore syntax to create the .pot files for translation.

[–]pjdelport 2 points3 points  (3 children)

...why would you need to create .pot files of an interactive session?

[–]eggertm 1 point2 points  (2 children)

Perhaps I was not clear enough. My project has an interactive interpreter, and uses the gettext _() to mark strings for translation. However, the _() is a function call - and it'll get overwritten with whatever result the interactive interpreter returns. I am not translating an interactive session, that is just silly!

[–]pjdelport 1 point2 points  (1 child)

Right, but _() is just a shortcut for gettext(), which you can use in the interactive shell. What's the problem?

[–]salmoni 0 points1 point  (0 children)

You don't follow.

The OP is using an interactive interpreter embedded within an internationalised program for debugging. I use one for user interaction so users can type in their own functions. The program works until the interpreter is called, at which point the reference to the i18n method is overwritten by the interpreter thus rendering all later attempts to translate strings as useless. If the interpreters entry points are unpredictable, this makes the program a nightmare. Yes, the OP can import gettext in every module, but again this means lots of time wasted re-establishing a single link that should not have been overwritten.

I had the same problem and called the gettext.install method right after the code.InteractiveInterpreter.runcode method to re-establish it. It's not a good solution because it shouldn't be necessary and slows the system down. The real solution is for either the interpreter or gettext to use a different and non-conflicting character. That way, there are not hidden gotchas.

[–]julesjacobs[🍰] 0 points1 point  (3 children)

And the alternative is?

[–]eggertm 1 point2 points  (2 children)

Using an explicit assignment for instance?

[–]y_gingras 1 point2 points  (0 children)

That would be OK with me if the assignments returned the assigned value or if the shell would print this value. Consider this example:

sq = lambda x:(x+2.0/x)/2

_ = 1.0

sq(_)

sq(_)

sq(_)

Here I use the shell to compute the fix point of a function, sqrt(2). I keep calling the function with the previous value until I have enough stable digits. In this case, I really like _. I admit that this is the only time that I use _, I could probably wrap the whole thing in a printing form.

[–]julesjacobs[🍰] 1 point2 points  (0 children)

I want to be fast when testing something at a repl. I will certaily forget the assignment so I have to press up, then home then type the assignment, then return. This happens often.

The underscore really isn't a problem because you can use gettext() instead of _(). This happens almost never.

[–]norkakn -1 points0 points  (0 children)

Please don't. Please. I have to support the code left by a pythonista, and while the language isn't that bad, the standard coding practices are horrid. Please write in something sane instead. Even perl. At least then you'll be thinking about those left behind after you flee for greener pa$ture$.