Path: csiph.com!eeepc.pasdenom.info!news.pasdenom.info!news.dougwise.org!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'string.': 0.04; 'int': 0.05; 'string,': 0.05; 'wed,': 0.05; 'infinite': 0.07; 'python': 0.08; 'conditional': 0.09; 'integers': 0.09; 'object.': 0.09; '>>>': 0.11; 'pm,': 0.12; ':-)': 0.12; 'am,': 0.13; 'jason': 0.13; 'loop': 0.13; 'wrote:': 0.14; 'def': 0.14; "'-'": 0.16; '(when': 0.16; '[snip]': 0.16; 'evaluates': 0.16; 'mrab': 0.16; 'negatives': 0.16; 'newcomer': 0.16; 'readable': 0.16; 'subject:function': 0.16; '\xa0this': 0.16; '\xa0you': 0.16; 'feb': 0.16; '(i.e.': 0.17; 'instead,': 0.18; 'cases': 0.21; 'number,': 0.21; 'fine': 0.22; 'object': 0.22; 'saying': 0.22; 'integer': 0.23; 'header:In-Reply-To:1': 0.23; 'mainly': 0.23; 'converting': 0.24; "python's": 0.24; 'skip:l 30': 0.24; 'subject:please': 0.24; 'code': 0.25; "isn't": 0.26; 'calling': 0.27; 'anyone': 0.27; 'string': 0.27; "i'm": 0.28; 'instead': 0.28; 'true,': 0.28; 'class': 0.29; 'message-id:@mail.gmail.com': 0.29; 'decimal': 0.30; 'language.': 0.30; 'to:addr:python-list': 0.31; 'actually': 0.31; 'thu,': 0.31; "doesn't": 0.32; 'however,': 0.32; 'suggestion': 0.33; 'depend': 0.34; 'gotten': 0.34; '10,': 0.35; 'several': 0.35; 'but': 0.36; 'later': 0.36; 'identity': 0.36; 'used': 0.36; 'test': 0.37; 'basic': 0.37; 'think': 0.37; 'works': 0.38; 'received:google.com': 0.38; 'fact': 0.38; 'should': 0.38; 'subject:: ': 0.39; 'sign': 0.39; 'to:addr:python.org': 0.40; 'could': 0.40; 'still': 0.40; "it's": 0.40; "you've": 0.61; 'your': 0.61; '12:03': 0.84; 'cosmetic': 0.84; 'exits.': 0.84; 'subject:Easy': 0.84; 'surface': 0.84; 'or:': 0.91 MIME-Version: 1.0 In-Reply-To: References: <8e0efff7-4d38-4a85-9653-2178388603d4@y36g2000pra.googlegroups.com> <87oc6l8z1y.fsf@rudin.co.uk> <4D53166D.6050103@mrabarnett.plus.com> Date: Thu, 10 Feb 2011 03:31:41 -0500 Subject: Re: Easy function, please help. From: Benjamin Kaplan To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 60 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1297326713 news.xs4all.nl 81485 [::ffff:82.94.164.166]:54118 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:55776 On Thu, Feb 10, 2011 at 12:03 AM, Jason Swails wro= te: > > > On Wed, Feb 9, 2011 at 5:34 PM, MRAB 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. =A0You'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). =A0This could encourage you in later cas= es >>> 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. =A0This works fine f= or >>> integers and positive numbers, but not so well for negatives and floats= , >>> since both the decimal and negative sign will be counted. =A0You could >>> typecast to a string then strip out '-' and '.' and then count the >>> characters. =A0i.e. >>> >>> def num_digits(n): >>> =A0 =A0return len(str(n).replace('-','').replace('.','')) >>> >> Or: >> >> def num_digits(n): >> =A0 =A0return 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?=A0 Most of my work ha= s to > be in Fortran, so I'm a relative newcomer to Python.=A0 When I don't need > Fortran-y performance it's much nicer (obviously to anyone that's used th= em > both)!=A0 Still don't know much deeper than Python's cosmetic surface at = this > point. > 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.