Programming intro book ch1 and ch2 (Windows/Python 3) - Request For Comments

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Fri Dec 18 22:36:51 EST 2009


On Fri, 18 Dec 2009 15:26:05 -0800, Mensanator wrote:

>> The second deviation is that since most names are constants,
> 
> Really? Does that mean you don't use literals, to save the time required
> to convert them to integers? Isn't that done at compile time?
> 
> So, instead of doing the Collatz Conjecture as
> 
> while a>1:
>   f = gmpy.scan1(a,0)
>   if f>0:
>     a = a >> f
>   else:
>     a = a*3 + 1
> 
> You would do this?
> 
> zed = 0
> one = 1
> two = 2
> twe = 3
> while a>one:
>   f = gmpy.scan1(a,zed)
>   if f>zed:
>     a = a >> f
>   else:
>     a = a*twe + one
> 
> Does this really save any time?

There are some people who might argue that using *any* magic constants in 
code is wrong, and that *all* such values should be declared as a 
constant.

It's easy to take the mickey out of such an extreme position:

zed = 0  # in case we need to redefine 0 as something else
one = 1  # likewise
two = 3  # changed from 2 to 3 to reflect the end of the Mayan calendar

# The following is guaranteed to pass unless the world is ending.
assert one+one == two-zed



-- 
Steven



More information about the Python-list mailing list