Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #55776

Re: Easy function, please help.

References (1 earlier) <87oc6l8z1y.fsf@rudin.co.uk> <c2cb4f8c-909e-4e47-b610-6d935c0c2ca1@w19g2000yqa.googlegroups.com> <AANLkTinsi3zGvcZSQySA3z1qZPn_tiT3q8BEBrErRpcZ@mail.gmail.com> <4D53166D.6050103@mrabarnett.plus.com> <AANLkTimw6N7vDtc4zoEw5TRznCeR1e0yGydz4AKiLM3E@mail.gmail.com>
Date 2011-02-10 03:31 -0500
Subject Re: Easy function, please help.
From Benjamin Kaplan <benjamin.kaplan@case.edu>
Newsgroups comp.lang.python
Message-ID <mailman.71.1297326713.1633.python-list@python.org> (permalink)

Show all headers | View raw


On Thu, Feb 10, 2011 at 12:03 AM, Jason Swails <jason.swails@gmail.com> wrote:
>
>
> On Wed, Feb 9, 2011 at 5:34 PM, MRAB <python@mrabarnett.plus.com> wrote:
>>
>> On 09/02/2011 21:42, Jason Swails wrote:
>>>
>>> You've gotten several good explanations, mainly saying that 0 -> False
>>> and not 0 -> True, which is why the while loop exits.  You've also
>>> gotten advice about how to make your method more robust (i.e. force
>>> integer division).
>>>
>>> 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).  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 (when instead it'll dump you into an infinite loop).
>>>
>>> You've also gotten the suggestion of typecasting to a string and then
>>> looking at the number of characters in the string.  This works fine for
>>> integers and positive numbers, but not so well for negatives and floats,
>>> since both the decimal and negative sign will be counted.  You could
>>> typecast to a string then strip out '-' and '.' and then count the
>>> characters.  i.e.
>>>
>>> def num_digits(n):
>>>    return len(str(n).replace('-','').replace('.',''))
>>>
>> Or:
>>
>> def num_digits(n):
>>    return len(str(abs(n)).replace('.',''))
>>
>>> Or typecast to an int if you want to neglect decimals before converting
>>> to a string, etc.
>>>
>> [snip]
>> Python doesn't have typecasting. :-)
>
> Because these basic types are not mutable?  <excuse> Most of my work has to
> be in Fortran, so I'm a relative newcomer to Python.  When I don't need
> Fortran-y performance it's much nicer (obviously to anyone that's used them
> both)!  Still don't know much deeper than Python's cosmetic surface at this
> point. </excuse>
>

Not exactly. It's because everything in Python is an object. What
you're doing isn't type casting. It's just calling an object
constructor- no different than any other class in the language.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Easy function, please help. Benjamin Kaplan <benjamin.kaplan@case.edu> - 2011-02-10 03:31 -0500

csiph-web