

Why Python Programmers Should Learn Python - bdfh42
http://wordaligned.org/articles/why-python-programmers-should-learn-python

======
BrandonM
Unfortunately, the example still contains duplicated information (and effort).
I would do:

    
    
      words = ["zero", "one", "two", "three", "four",
               "five", "six", "seven", "eight", "nine"]
    
      numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    
      number_to_word = dict(zip(numbers, words))
      word_to_number = dict(zip(words, numbers))
    

The rest, of course, can remain the same. I think adding this would also help
to show how Python idioms like zip can help simplify code.

That said, I still voted the submission up. I've seen several articles like
this which point out how people use new languages in the same braindead way
they used their old languages, yet I feel this one featured a very simple
example with a paradigm that maps well to Python.

EDIT: Whoops, I got distracted and didn't realize that I didn't finish reading
the article. It turns out that the approach is modified later on to do
something even better, but I'll leave you to check out the article for that.

~~~
mrtron
numbers = range(10) is mucho easier than typing the numbers :)

    
    
        words = ["zero", "one", "two", "three", "four",
               "five", "six", "seven", "eight", "nine"]
        number_to_word = dict(zip(range(10), words))
        word_to_number = dict(zip(words, range(10)))
    

The last approach with lots of testing is good.

~~~
BrandonM
Well, here's the approach that was settled on in the article:

    
    
      import string
      words = "zero one two three four five six seven eight   nine".split()
      number_to_word = dict(zip(string.digits, words))
      word_to_number = dict(zip(words, range(10)))
    

I'm not sure why string.digits was used in the first zip but not the second.
As kirubakaran pointed out, though, the number_to_word dictionary isn't even
needed. The best approach, in my opinion, becomes:

    
    
      number_to_word = "zero one two three four five six seven eight nine".split()
      word_to_number = dict(zip(number_to_word, range(10))
    

This would serve to display several cool Python idioms: the simplicity of
string splitting, the power of zip, the simplicity of producing a list of
numbers, and the use of the accessor on different types of data structures. If
memory recalls, none of those operations are that easy to do in Java.

~~~
dood
It can be made a little neater, show off a little more of Python and be more
versatile by replacing range(10) with count() from the iterator module
[<http://docs.python.org/lib/itertools-functions.html>]

    
    
        from itertools import count
        words = "zero one two three four five six seven eight nine".split()
        word_to_number = dict(zip(words, count()))
    

[<http://codepad.org/naCDzno7>]

~~~
BrandonM
I like itertools as much as the next programmer (although a large subset of it
will be subsumed when Python3k switches to iterators on operations like map
and zip), but I feel like using an imported module to (apparently) produce a
list of numbers might actually give the _wrong_ idea about Python. In several
other languages, you can't even write to standard output without importing a
module or five, whereas in Python, you can get a lot done without even
touching the standard library (although you will likely want to rather
quickly). Seeing an import statement at the top might give the Python newbie
the idea that core Python doesn't have "basic" functionality.

I think a good later lesson would be to explain how iterators and the standard
library works and then revisit this code to make it look like your example,
but in the beginning, I think range(10) would be simpler and would better
serve the purpose of the lesson. Even in this case, xrange (at least in Python
2.x) is obviously the better choice, but introducing that complexity at this
point doesn't gain much, in my opinion.

~~~
eru
Won't range behave like xrange soon anyway?

------
jcromartie
There's nothing more maddening than seeing someone write Java in Python. I see
it all over the place here, with giant utility classes that should just be
modules, instance variables instead of locals (yes, that means `self.foo`
where `foo` would work exactly the same).

------
Tichy
Just a note, Java has split and replace for Strings, too...

