PEP308: Yet another syntax proposal
holger krekel
pyth at devel.trillke.net
Mon Feb 10 15:02:40 EST 2003
David Eppstein wrote:
> On 2/10/03 7:36 PM +0100 holger krekel <pyth at devel.trillke.net> wrote:
> >
> > right. in which case learning about and/or (which you have to anyway)
> > often helps. Because of the "y is false" problem in "x and y or z"
> > there are some cases where it doesn't fit. Do we have real code
> > examples for them?
>
> I posted one several hundred messages back:
> cols[i] if i >= 0 else None
>
> It's from real code, an example where the and/or trick fails because you
> don't know the truth status of cols[i], and an example where the guard
> might serve to prevent an array out of bounds exception.
ok. I think i have seen that before but also thought
that list/tuple show grow a 'get' method (parallel to
the dict.__getitem__ / dict.get pair).
And i thought why would someone really need to do this
check? Either you allow negative indices (which work on
list/tuples) or you don't in which case you might like
to get into some error-path anyway. How do you get
to 'i<0' anyway?
Apart from this specific example your general perception is interesting ...
> >> My feeling is that the reason for short circuiting isn't so much because
> >> of side effects (I think we should try to keep expressions as free of
> >> side effects as possible) but because one or both of the two sides may
> >> be undefined and raise an exception if the guard condition is not met.
This reinforces an earlier thought about PEP-308 alternatives,
which might read as follows:
cols[i] except IndexError: None
Beeing able to deal with exceptions within expressions like this
would not obsolete other expressions and is unlikely to obfuscate code.
It feels more pythonic to me because it avoids the "look before you
leap" idiom, i.e. you don't have to know anything specific about
your keys and do checks to them. Just try and define the exceptional
result.
regards,
holger
More information about the Python-list
mailing list