Easy function, please help.

Jason Swails jason.swails at gmail.com
Thu Feb 10 00:26:21 EST 2011


On Wed, Feb 9, 2011 at 8:16 PM, Ethan Furman <ethan at stoneleaf.us> wrote:

> Jason Swails wrote:
>
>> However, as surprising as this may be I'm actually with RR on this one
>> (for a little) -- for code readability's sake, you should make your
>> conditional more readable (i.e. don't depend on the fact that the iterations
>> will take your test value down to 0 which conveniently in this case
>> evaluates to False).
>>
>
> while n:  is plenty readable.  n is either something or nothing, and
> something evaluates to True, nothing to False.
>

Sure it's readable.  But then you have to make sure that the loop will
eventually take n down to 0.  You can always *assume* that the programmer
knew what they were doing (not an assumption I'm typically willing to make
on code that's not my own).

How is "while n != 0:" any worse?  (or abs(n) < tolerance).  It has exactly
the same effect without adding any code while at the same time directly
communicates the intended conditional.  IMO it makes reading the code easier
to read barring effective documentation (my experience with people
documenting their code is that they don't; at least in my field).  "while n
!= 0" makes my life easier.


>  This could encourage you in later cases to think that if this result
>> eventually converged to a different number, say the multiplicative identity
>> instead, that the same approach will work [...]
>>
>
> See above comment -- something or nothing, not mathematical identities.


The fact that the proposed loop finished with *nothing* was coincidental.
What if he had been doing some type of prime factorization or something
where each iteration reduced the number until eventually all you were left
with was the multiplicative identity?  You'd say (correctly) that obviously
the same approach won't work, but in some lines of thought it's a logical
extension to use the same construct (especially to those that don't fully
understand why the original loop exits in the first place).  Expanding the
conditional a little can only help IMO.


>
>  def num_digits(n):
>>   return len(str(n).replace('-','').replace('.',''))
>>
>> Or typecast to an int if you want to neglect decimals before converting to
>> a string, etc.
>>
>> Or use recursion!
>>
>>  >>> def num_digits(n):
>> ...    if n == 0:
>> ...       return 0
>> ...    else:
>> ...       return num_digits(n//10) + 1
>> ...
>>  >>> num_digits(1)
>> 1
>>  >>> num_digits(0)
>> 0
>>
>
> 0 is still one digit.  ;)
>

Well that is something; yet only nothing evaluates to False.  We seem to be
at an impasse :).

--Jason


> ~Ethan~
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
Jason M. Swails
Quantum Theory Project,
University of Florida
Ph.D. Graduate Student
352-392-4032
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110210/8244ad02/attachment.html>


More information about the Python-list mailing list