PEP308: Yet another syntax proposal
Andrew Dalke
adalke at mindspring.com
Mon Feb 10 19:35:27 EST 2003
James J. Besemer:
> Andrew Dalke wrote:
> > Raymond Hettinger
> > ....
> > Err, that should have been
> >
> > [ spelled out long-hand ]
> Now you're being unreasonable.
No, I'm not being unreasonable. Raymond used
data = isinstance(source, file) ?? source.readlines() ||
source.split()
My post was not commenting about the use of the ?? || syntax,
it was saying that explicit type checking should not be used for
this case.
Specifically, don't check for a file instance, check to see if it supports
a 'readlines'. As written, it prevents me from using a StringIO class
or anything else which implements 'readlines.'
At best, do the type check only for the most restrictive case
(which is a string) rather than the least restrictive case (a file-like
object).
The fact that I wrote my objection using the traditional if/else has
nothing to do with the point of the matter.
> You were asked for an example where short-circuit would be necessary and
this
> is a well-intentioned, legitamet illustration.
>
> The fact (as you point out and WE ALL KNOW) that it can be rewritten the
> other way does not materially add to the discussion.
The test should have been, in his parlance,
data = hasattr(source, "readlines") ?? source.readlines() ||
source.split()
Even with that case, I would rather not get that property twice,
so, using Alex's clarification of my example, the best solution is
try:
readlines = source.readlines
except AttributeError:
data = source.split()
else:
data = readlines()
Therefore, even though I was not really commenting on the use
of an if/else expression idiom, it happens that this example is not
legitamet .. legtiment, .. legit-whatever.
Andrew
dalke at dalkescientific.com
More information about the Python-list
mailing list