Live data from Hacker News

Python idioms I wish I'd learned earlier

prooffreaderplus.blogspot.com

171–174 of 174 posts

Re: Python idioms I wish I'd learned earlier

#171
There is an sweet in India called [0] phirni when you start eating from outer layer to inner layer, you will feel like walking in heaven.Now you are in outer layer.I hope you enter to the inner level and feel the python still. :)

[0] http://www.wikihow.com/Make-Phirni-%28a-Rice-and-Milk-Dish%2...

Re: Python idioms I wish I'd learned earlier

#172
post #22

This is something I do instead of writing a long if-else: opt = {0: do_a, 1: do_b, 3: do_b, 4: do_c} opt[option]()

Yes, as SixSigma says, it's called a jump table; I've also seen it called a dispatch table (because you dispatch control to the right function based on a key). It's quite an old technique, dating from the earliest programming languages and even used in assembly language. using indirect addressing and suchlike techniques.

Edit: Looked it up, dispatch table is in Wikipedia:

http://en.wikipedia.org/wiki/Dispatch_table

Re: Python idioms I wish I'd learned earlier

#173
post #88

Earlier quoted context omitted.

> 1. Am I the only one that really loves that `print` is a statement and not a function? Call me lazy, but I don't mind not having to type additional parentheses. If you think the parentheses are bad, why wouldn't you prefer a language like Ruby where you can omit them generally? Leaving them out for just one special construct seems so insufficient as a cure.

In general, Ruby's syntax (or rather, semantics) is ambiguous (you don't know if a statement without parentheses is calling a function or just accessing a value). I prefer unambiguous syntax. However, Python's `print` statement is (1) well-known, (2) very useful (for prototyping, debugging, in REPL), and (3) shouldn't in general be present in production code (logging should be used instead). Therefore, omitting paren…

Try %autocall in ipython. `

In [9]: %autocall Automatic calling is: Smart

In [10]: def foo(a, b): return a + b ....:

In [11]: foo 3, 4 -------> foo(3, 4) Out[11]: 7

`

Re: Python idioms I wish I'd learned earlier

#174

If you were underwhelmed by this blog post have a look at: Transforming code into Beautiful, Idiomatic Python by Raymond Hettinger at PyCon 2013 https://speakerdeck.com/pyconslides/transforming-code-into-b... and https://www.youtube.com/watch?v=OSGv2VnC0go&noredirect=1 Topics include: 'looping' with iterators to avoid creating new lists, dictionaries, named tuples and more

Sorry for the offtopic but why did you add `&noredirect=1` to the end of that youtube url?

sorry, was only an accident.
Post reply on HN