What's better about Ruby than Python?
Alex Martelli
aleax at aleax.it
Thu Aug 21 03:45:40 EDT 2003
POYEN OP Olivier (DCL) wrote:
>> Thus, the direct Python equivalent might be
>>
>> import __builtins__
>> __builtins__.len = someotherfunction
>>
>> and THAT usage is very specifically warned against e.g. in
>> "Python in a Nutshell" -- you CAN do it but SHOULDN'T (again, one
>> can hope to eventually see it "officially" deprecated...!).
>
>
> Would that mean the end of
> import sys
> sys.stdout=myLogger()
> ...
> print 'it works'
> ...
> sys.stdout=sys.__stdout__
A dangerous idiom -- you should definitely use try/finally here,
otherwise your sys.stdout will stay wrongly bound if there is an
exception soon after it. But with try/finally, or hopefully some
syntax tweak making try/finally semantics even easier to use, this
is a kind of idiom that's gonna stay. sys is FULL of entries
intended mainly as hooks for you to replace, by the way -- all
of those which are named sys.<somethingorother>hook:-).
> Sad, sad, sad. Quite useful in some way.
> But I assume it could be replace by the new
> print >>>loggerInstace, "it works also, but is longer"
<shudder> not so new any more, but still my least-favourite
Python construct. It doesn't replace temporary rebinding of
sys.stdout to capture output from existing pieces of code
in a *non-invasive* way, anyway; it's meant to be used instead
of such rebinding when you need to divert output only for a
brief span of code and don't mind being 'invasive' in that code.
Alex
More information about the Python-list
mailing list