Live data from Hacker News

Python Language Features and Tricks

sahandsaba.com

21–30 of 86 posts

Re: Python Language Features and Tricks

#21
post #14

zip to unzip a dict is a very slow approach to do it Instead of mi = dict(zip(m.values(), m.keys())) Do mi = {v: k for (k, v) in m.iteritems()}

When did support for dictionary comprehensions make it into 2.x? I could've sworn it didn't used to work, but I just tried it in the shell and sure enough, it does in 2.7.3.

2.7:

http://legacy.python.org/dev/peps/pep-0274/

Re: Python Language Features and Tricks

#23
post #4

patterns / tricks = language deficiencies Wake me up when Python will support tail call elimination and will get rid of GIL. For now this language is no better than PHP.

I suggest you are suffering from the Blub Paradox[0][1][2].

[0] http://paulgraham.com/avg.html

[1] http://c2.com/cgi/wiki?BlubParadox

[2] http://weblog.raganwald.com/2006/10/are-we-blub-programmers....

Re: Python Language Features and Tricks

#25

Earlier quoted context omitted.

For TRE: http://neopythonic.blogspot.com/2009/04/tail-recursion-elimi... For the GIL, there are alternative implementations of Python (Jython, IronPython, etc.)

Hm... Python only supports 1000 recursions? That seems unsafe. It seems like anyone who writes functional-style code will run the risk of a stack overflow.

You can increase the recursion limit like this:

import sys

sys.setrecursionlimit(x)

Kind of a "hackish" way to do things but you can do it if you need to.

Re: Python Language Features and Tricks

#26
Coming from a long history of languages like BASIC and Pascal, I will bookmark this tutorial. It seems to open up a lot of interesting Python features that were, quite frankly, not always easy to understand when described in plain text, but now seem pretty simple when presented as examples.

I'll also think about the "collection of simple examples" next time I want to document something.

Re: Python Language Features and Tricks

#27
post #7

I have two questions. 1. I'm unfamiliar with the term 'unpacking'. Is it any different from pattern matching in, say, Haskell (but perhaps not as feature-rich)? 2. Aren't slices pretty much a staple in Python? I didn't think using them was considered a 'trick'.

Unpacking is a limited form of what is called destructuring in other languages like Clojure. I would say that, in terms of feature-richness: unpacking < destructuring < pattern matching.

I'd extend that one step further

    unpacking 
where first-class patterns are increasingly becoming available in some languages which offer pattern matching (in particular, Haskell will have them soon).

Re: Python Language Features and Tricks

#28
Slice has always been a painful adventure for me. I always forget that [1:3] is not all inclusive. It's actually just range from 1 to 2.

I believe in 2.7 zip is still returning a list rather than an iterator (izip in Python 2, zip in Python 3+).

Another unappreciated stdlib is definitely functools. mock is also another awesome stdlib.

functools, collections and itertools are definitely useful to make things faster. Also check out the list of stdlib. http://docs.python.org/2/library/

Re: Python Language Features and Tricks

#29
post #14

zip to unzip a dict is a very slow approach to do it Instead of mi = dict(zip(m.values(), m.keys())) Do mi = {v: k for (k, v) in m.iteritems()}

more importantly it's wrong - the iteration order of .values() and .keys() is not guaranteed to be consistent

Re: Python Language Features and Tricks

#30
Nice reference.

1.29 happened to be exactly what I was looking for:

  for subset in itertools.chain(*(itertools.combinations(a, n) for n in range(len(a) + 1)))
I spent way too much time writing a function to come up with these combinations.
Post reply on HN