Python riddle

Dan Bishop danb_83 at yahoo.com
Tue Dec 6 01:58:42 EST 2005


kyle.tk wrote:
> SPE - Stani's Python Editor wrote:
> > I know that this code is nonsense, but why does this print 'Why?'
> >
> > a = 1
> > if a >2:
> >     try:
> >         5/0
> >     except:
> >         raise
> > else:
> >     print 'why?'
>
> last time i checked this should print 'why?'
> I have no idea how you got it to print 'Why?'

>>> class CapOutput:
...    def __init__(self, fileobj):
...       self._file = fileobj
...    def write(self, text):
...       self._file.write(text.capitalize())
...
>>> sys.stdout = CapOutput(sys.stdout)
>>> print 'why?'
Why?




More information about the Python-list mailing list