Re: Proposal for adding symbols within Python



On Tue, 15 Nov 2005 01:57:53 -0800, Ben Sizer wrote:

>> I don't think I even understand what the objection is. What is
>> needed is a code fragment that shows how the use of strings is
>> untenable.
>
> myObject.value = 'value1'
>
> #... 100 lines of code elided...
>
> if myObject.value = 'Value1':
> do_right_thing()
> else:
> do_wrong_thing()
>
>
> I don't actually think string use is 'untenable', but it is definitely
> more error-prone. With some sort of named object on the right hand side
> you will at least get a helpful NameError.

It is moments like this that I'm not too proud to admit I learnt some good
techniques from Pascal:

# define some pseudo-constants
RIGHT_THING = 'value1'
WRONG_THING = 'some other value'
INCHES_TO_FEET = 12
CM_TO_METRES = 100

# ...

myObject.value = RIGHT_THING

#... 100 lines of code elided...

if myObject.value = RIGHT_THING:
do_right_thing()
else:
do_wrong_thing()


It isn't always appropriate or necessary to define "constants" (and I
sometimes wish that Python would enforce assign-once names), but they can
help avoid some silly mistakes.



--
Steven.

.



Relevant Pages

  • Re: while (1) vs. for ( ;; )
    ... > To bad grep doesn't know how to ignore C comments and literals. ... or is this just a silly objection for the ... Use an editor and fix the problem. ... In cases where 'while' appears inside strings (or ...
    (comp.lang.c)
  • Re: infinity
    ... there is provably no bijection between A and P ... Just consider bit strings consisting of one bit in each ... >> The same objection ... denominator has only factors of the radix has dual representation. ...
    (sci.math)
  • Re: Stream Writer not writing full array
    ... The code fragment is a bit lacking in details, but the oasis_info array ... If these objects aren't Strings, ... Elisa ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: UIT End Revision: A challenging sub-sport
    ... just contradicted twenty billion strings of objection by agreeing it ... I periodically play goaltimate with some of the best ... ultimate players in the world. ...
    (rec.sport.disc)