Ben Finney wrote:
Peter Pearson <ppear...@nowhere.invalid> writes:

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?

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()

Strings may have their use, most of the time providing a string representation of the object, here is an example:

FORWARD = object()

print 'moving %s' % FORWARD
> moving <object object at 0xb7dab5a8>

Another approach using strings:

class Direction:
   FORWARD = 'forward'

print "moving %s' % Direction.FORWARD
> moving forward

Note that
Direction.FORWARD is Direction.FORWARD
is safe and will return True.

JM




--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to