Convert from numbers to letters
Steven Bethard
steven.bethard at gmail.com
Thu May 19 12:40:24 EDT 2005
Bill Mill wrote:
>py> alpha = 'abcdefghijklmnopqrstuvwxyz'
>py> for i, digraph in enumerate(sorted([''.join((x, y)) for x in alpha
> ... for y in [''] + [z for z in alpha]], key=len)):
> ... locals()[digraph] = i + i
> ...
It would probably be better to get in the habit of writing
globals()[x] = y
instead of
locals()[x] = y
You almost never want to do the latter[1]. The only reason it works in
this case is because, at the module level, locals() is globals().
You probably already knew this, but I note it here to help any newbies
avoid future confusion.
Steve
[1] For 99% of use cases. Modifying locals() might be useful if you're
just going to pass it to another function as a dict. But I think I've
seen *maybe* 1 use case for this.
More information about the Python-list
mailing list