easy question on parsing python: "is not None"
Peter Pearson
ppearson at nowhere.invalid
Fri Aug 6 13:20:30 EDT 2010
On Fri, 06 Aug 2010 15:37:04 +0200, Stefan Schwarzer wrote:
[snip]
> I can imagine a case where you might want to compare a
> string with `is`:
>
> FORWARD = "forward"
> BACKWARD = "backward"
>
> ...
>
> def func(direction=FORWARD):
> if direction is FORWARD:
> ...
> elif direction is BACKWARD:
> ...
> else:
> ...
>
> in case you expect people to specifically use the constants
> you provided in the module. Here, the fact that FORWARD
> actually is the string "forward" might be considered an
> implementation detail. Using a string instead of an
> `object()` has the advantage that it makes usage in error
> messages easier.
>
> Actually, I've never seen such a use, as far as I remember.
> What do other people here think? Is the code above, which
> compares strings with `is`, bad style, and if yes, why? How
> would you write the code instead?
Hey, that's a cute example, but . . . what a trap! Is it
possible to document the use-the-object-not-the-string requirement
loudly enough that people won't get caught?
--
To email me, substitute nowhere->spamcop, invalid->net.
More information about the Python-list
mailing list