Live data from Hacker News

Python Language Features and Tricks

sahandsaba.com

31–40 of 86 posts

Re: Python Language Features and Tricks

#31
A good reference, to be sure, but man, do I resent the term “trick” in programming. It implies a deception, or something clever that you wouldn’t think to look for, like opening a wine bottle with a shoe. These aren’t tricks, they’re (largely) standard library features that you would simply expect to exist. But maybe I’m underestimating the NIH effect.

Re: Python Language Features and Tricks

#32
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

The order of items in the dictionary isn't stable, but the order of .keys() is guaranteed to be the same as the order of .values() (as long as you don't modify the dict in between calling one then the other).

http://docs.python.org/2/library/stdtypes.html#dict.items

Re: Python Language Features and Tricks

#34
post #6

Earlier quoted context omitted.

I think unpacking is just unpacking, languages that have pattern matching allow you to do something identical: # let i = (1,2,3) ;; val i : int * int * int = (1, 2, 3) # let a,b,c = i;; val a : int = 1 val b : int = 2 val c : int = 3

Unpacking pretty much does one thing, and it makes that one thing easier and a lot more readable. Combine that with list comprehension (from what I understand C#'s LINQ is similar) and you end up with code that's highly maintainable/readable (as long as you name your variables appropriately, of course). I believe Python supports pattern matching other than Regex as well.

Python list comprehensions are not lazy, though. If you generate [for x in xrange(1, 10000)] you'll get 10000 elements in your list. My understanding is that LINQ list comprehensions are that.

> I believe Python supports pattern matching other than Regex as well.

That's pattern matching on strings. The kind of pattern matching being discussed here is pattern matching on data types (see [1]).

1: http://en.wikipedia.org/wiki/Pattern_matching

Re: Python Language Features and Tricks

#35

Earlier quoted context omitted.

Unpacking pretty much does one thing, and it makes that one thing easier and a lot more readable. Combine that with list comprehension (from what I understand C#'s LINQ is similar) and you end up with code that's highly maintainable/readable (as long as you name your variables appropriately, of course). I believe Python supports pattern matching other than Regex as well.

Python list comprehensions are not lazy, though. If you generate [for x in xrange(1, 10000)] you'll get 10000 elements in your list. My understanding is that LINQ list comprehensions are that. > I believe Python supports pattern matching other than Regex as well. That's pattern matching on strings. The kind of pattern matching being discussed here is pattern matching on data types (see [1]). 1: http://en.wikipedia.or…

'Lazy lists' are called generators in Python. Just use (...) Instead of [...] in your example.

Re: Python Language Features and Tricks

#36

Earlier quoted context omitted.

Unpacking pretty much does one thing, and it makes that one thing easier and a lot more readable. Combine that with list comprehension (from what I understand C#'s LINQ is similar) and you end up with code that's highly maintainable/readable (as long as you name your variables appropriately, of course). I believe Python supports pattern matching other than Regex as well.

Python list comprehensions are not lazy, though. If you generate [for x in xrange(1, 10000)] you'll get 10000 elements in your list. My understanding is that LINQ list comprehensions are that. > I believe Python supports pattern matching other than Regex as well. That's pattern matching on strings. The kind of pattern matching being discussed here is pattern matching on data types (see [1]). 1: http://en.wikipedia.or…

Python does have generator expressions, for instance what you wrote above is written (lazily) as:(x for x in xrange(1, 10000))

It doesnt have functional style pattern matching though.

Re: Python Language Features and Tricks

#37
I think this is great, I've been doing Python for a while and I knew many of the features but I also learned a few new ones.

I don't understand how this one to flatten lists works:

    a = [[1, 2], [3, 4], [5, 6]]
    [x for l in a for x in l]
Can somebody explain what the order of operations is here and what the variables refer to in the various stages of evaluation?

Re: Python Language Features and Tricks

#38
post #35

Earlier quoted context omitted.

Python list comprehensions are not lazy, though. If you generate [for x in xrange(1, 10000)] you'll get 10000 elements in your list. My understanding is that LINQ list comprehensions are that. > I believe Python supports pattern matching other than Regex as well. That's pattern matching on strings. The kind of pattern matching being discussed here is pattern matching on data types (see [1]). 1: http://en.wikipedia.or…

'Lazy lists' are called generators in Python. Just use (...) Instead of [...] in your example.

I'm aware of generator expressions, but the fact that list comprehensions are not lazy can trip people if they are used to other languages with this feature, since they are usually lazy.

Re: Python Language Features and Tricks

#39

I think this is great, I've been doing Python for a while and I knew many of the features but I also learned a few new ones. I don't understand how this one to flatten lists works: a = [[1, 2], [3, 4], [5, 6]] [x for l in a for x in l] Can somebody explain what the order of operations is here and what the variables refer to in the various stages of evaluation?

The expression is a list comprehension with 2 nested for statements. It is similar to this, which names its result:

   result = []
   for l in a:
      for x in l:
         result.append(x)

Re: Python Language Features and Tricks

#40
post #25

Earlier quoted context omitted.

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.

^Although I think the idea is that you shouldn't need to. Any recursive implementation can either be easily translated to work iteratively, or can be implemented in such a way that 1000 levels of recursion should be more than enough.

For example, 1000 levels of recursion is more than enough to count all the nodes in a binary tree unless the tree is extremely poorly balanced or inordinately large.

Post reply on HN