Python Mystery Theatre -- Episode 2: Así Fue

Raymond Hettinger vze4rx4y at verizon.net
Mon Jul 14 01:42:13 EDT 2003


Here are four more mini-mysteries for your amusement
and edification.

In this episode, the program output is not shown.
Your goal is to predict the output and, if anything
mysterious occurs, then explain what happened
(again, in blindingly obvious terms).

There's extra credit for giving a design insight as to
why things are as they are.

Try to solve these without looking at the other posts.
Let me know if you learned something new along the way.

To challenge the those who thought the last episode
was too easy, I've included one undocumented wrinkle
known only to those who have read the code.


Enjoy,


Raymond Hettinger



ACT I -----------------------------------------------
print '*%*r*' % (10, 'guido')
print '*%.*f*' % ((42,) * 2)

ACT II -----------------------------------------------
s = '0100'
print int(s)
for b in (16, 10, 8, 2, 0, -909, -1000, None):
    print b, int(s, b)

ACT III ----------------------------------------------------
def once(x): return x
def twice(x): return 2*x
def thrice(x): return 3*x
funcs = [once, twice, thrice]

flim = [lambda x:funcs[0](x), lambda x:funcs[1](x), lambda x:funcs[2](x)]
flam = [lambda x:f(x) for f in funcs]

print flim[0](1), flim[1](1), flim[2](1)
print flam[0](1), flam[1](1), flam[2](1)

ACT IV ----------------------------------------------------
import os
os.environ['one'] = 'Now there are'
os.putenv('two', 'three')
print os.getenv('one'), os.getenv('two')







More information about the Python-list mailing list