easy question on parsing python: "is not None"

Stefan Schwarzer sschwarzer at sschwarzer.net
Sun Aug 8 03:46:40 EDT 2010


Hi Ben,

On 2010-08-08 01:16, Ben Finney wrote:
> Don't use strings for such values. The data isn't going to be used, so
> there's no sense using a semantically rich data type like a string.
> 
> Instead, use an ‘object’ instance; then, the only way to get a binding
> that will compare equal is to use the very object itself.
> 
> FORWARD = object()
> BACKWARD = object()

Yes, that's the obvious approach. :-) I had used strings in
the example with the reasoning that they could be readily
used in messages (from my post: "Using a string instead of
an `object()` has the advantage that it makes usage in error
messages easier.").

For the record, another approach I've seen is something like

FORWARD, BACKWARD, UNKNOWN = range(3)

This has the same problem as when using strings for the
constants but looking at the module contents it's a bit
more obvious that these are just "magic numbers", so
presumably the constant names should be used instead.

Stefan



More information about the Python-list mailing list