easy question on parsing python: "is not None"

Albert van der Horst albert at spenarnc.xs4all.nl
Sun Aug 8 11:12:16 EDT 2010


In article <8c2uiuFg9bU1 at mid.individual.net>,
Peter Pearson  <ppearson at nowhere.invalid> wrote:
>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?

king = "This is just a string for denoting a chess object, \
if a field contains (a reference to) king, it means the piece \
is standing on that field"

Even a fresh convert from the Java language would not insert the
string instead of the name of the object.

(In practive you want black kings and white kings to be different
objects, probably.)

>
>--
>To email me, substitute nowhere->spamcop, invalid->net.


--
-- 
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- being exponential -- ultimately falters.
albert at spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst




More information about the Python-list mailing list