Re: [Tutor] Why begin a function name with an underscore

2012-09-01 Thread Wayne Werner
On Thu, 30 Aug 2012, Steven D'Aprano wrote: On 28/08/12 21:24, Wayne Werner wrote: On Mon, 27 Aug 2012, Richard D. Moores wrote: What the best way to test if something's an integer? try: whatever_you_want(supposed_integer) except ValueError: print("Oops, that wasn't an integer! Ple

Re: [Tutor] Why begin a function name with an underscore

2012-08-30 Thread Steven D'Aprano
On 31/08/12 06:57, eryksun wrote: On Thu, Aug 30, 2012 at 2:07 PM, Peter Otten<__pete...@web.de> wrote: Allowing floats for a primality test is a can of worms anyway. You will inevitably run out of significant digits: Allowing floats can also lead to type errors for operations that require a

Re: [Tutor] Why begin a function name with an underscore

2012-08-30 Thread eryksun
On Thu, Aug 30, 2012 at 2:07 PM, Peter Otten <__pete...@web.de> wrote: > > Allowing floats for a primality test is a can of worms anyway. You will > inevitably run out of significant digits: Allowing floats can also lead to type errors for operations that require an integral type, but at least the

Re: [Tutor] Why begin a function name with an underscore

2012-08-30 Thread Steven D'Aprano
On 31/08/12 04:07, Peter Otten wrote: Allowing floats for a primality test is a can of worms anyway. You will inevitably run out of significant digits: [snip] Yes, I had more or less come to the same conclusion earlier. The problem is that although sufficiently large floats are all integer-va

Re: [Tutor] Why begin a function name with an underscore

2012-08-30 Thread Peter Otten
Steven D'Aprano wrote: > On 28/08/12 19:02, Peter Otten wrote: >> Personally, I'm a big fan of ducktyping, so I would probably remove the >> check completely and live with the consequences: >> > >>> pyprimes._validate_int = lambda x: None > >>> pyprimes.isprime_naive(8.5) >> True >> >> g

Re: [Tutor] Why begin a function name with an underscore

2012-08-30 Thread eryksun
On Thu, Aug 30, 2012 at 4:44 AM, Peter Otten <__pete...@web.de> wrote: > sum(["a", "b", "c"], "") > Traceback (most recent call last): > File "", line 1, in > TypeError: sum() can't sum strings [use ''.join(seq) instead] > > gives me the creeps even though it'd never occur to me to actually

Re: [Tutor] Why begin a function name with an underscore

2012-08-30 Thread Peter Otten
Steven D'Aprano wrote: > On 28/08/12 20:00, Peter Otten wrote: > [...] >> The differences to _validate_int() are subtle: >> > class S(str): >> ... def __eq__(self, other): return True >> ... def __ne__(self, other): return False >> ... def __add__(self, other): return self >> ... >

Re: [Tutor] Why begin a function name with an underscore

2012-08-29 Thread Steven D'Aprano
On 30/08/12 05:39, Japhy Bartlett wrote: If you tried to check in code like this: def _validate_int(obj): """Raise an exception if obj is not an integer.""" m = int(obj + 0) # May raise TypeError. if obj != m: raise ValueError('expected an integer but got %r' % obj

Re: [Tutor] Why begin a function name with an underscore

2012-08-29 Thread Steven D'Aprano
On 28/08/12 21:24, Wayne Werner wrote: On Mon, 27 Aug 2012, Richard D. Moores wrote: What the best way to test if something's an integer? try: whatever_you_want(supposed_integer) except ValueError: print("Oops, that wasn't an integer! Please try again") That's usually the best way..

Re: [Tutor] Why begin a function name with an underscore

2012-08-29 Thread Steven D'Aprano
On 28/08/12 20:00, Peter Otten wrote: [...] The differences to _validate_int() are subtle: class S(str): ... def __eq__(self, other): return True ... def __ne__(self, other): return False ... def __add__(self, other): return self ... vi(S("42")) Traceback (most recent call last):

Re: [Tutor] Why begin a function name with an underscore

2012-08-29 Thread Steven D'Aprano
On 28/08/12 19:02, Peter Otten wrote: Personally, I'm a big fan of ducktyping, so I would probably remove the check completely and live with the consequences: >>> pyprimes._validate_int = lambda x: None >>> pyprimes.isprime_naive(8.5) True garbage-in, garbage-out -- so what. Duck-typing

Re: [Tutor] Why begin a function name with an underscore

2012-08-29 Thread Steven D'Aprano
On Mon, Aug 27, 2012 at 6:33 PM, Japhy Bartlett wrote: something like: def _validate_int(obj): """Raise an exception if obj is not an integer.""" m = int(obj + 0) # May raise TypeError. if obj != m: raise ValueError('expected an integer but got %r' % obj) is a really

Re: [Tutor] Why begin a function name with an underscore

2012-08-29 Thread Steven D'Aprano
On 28/08/12 10:26, Richard D. Moores wrote: I've been going through Steven D'Aprano's pyprimes () and have many questions. One is why begin a function name with an underscore. He has several functions that do this. Two are: [...] I'm stealing these f

Re: [Tutor] Why begin a function name with an underscore

2012-08-28 Thread eryksun
On Tue, Aug 28, 2012 at 9:08 AM, Peter Otten <__pete...@web.de> wrote: > > That would reject "floats with an integral value" and therefore doesn't > comply with the -- non-existing -- spec. Gotcha. >> >>> import numbers >> >>> isinstance("42", numbers.Integral) >> False >> >>> num

Re: [Tutor] Why begin a function name with an underscore

2012-08-28 Thread Peter Otten
eryksun wrote: > On Tue, Aug 28, 2012 at 6:00 AM, Peter Otten <__pete...@web.de> wrote: >> >> Anyway here's an alternative implementation: >> > def vi(x): >> ... if not isinstance(x, numbers.Number): >> ... raise TypeError >> ... if not int(x) == x: >> ... raise

Re: [Tutor] Why begin a function name with an underscore

2012-08-28 Thread eryksun
On Tue, Aug 28, 2012 at 6:00 AM, Peter Otten <__pete...@web.de> wrote: > > Anyway here's an alternative implementation: > def vi(x): > ... if not isinstance(x, numbers.Number): > ... raise TypeError > ... if not int(x) == x: > ... raise ValueError You could tes

Re: [Tutor] Why begin a function name with an underscore

2012-08-28 Thread Wayne Werner
On Mon, 27 Aug 2012, Richard D. Moores wrote: On Mon, Aug 27, 2012 at 6:33 PM, Japhy Bartlett wrote: something like: def _validate_int(obj): """Raise an exception if obj is not an integer.""" m = int(obj + 0) # May raise TypeError. if obj != m: raise ValueError('expected

Re: [Tutor] Why begin a function name with an underscore

2012-08-28 Thread Peter Otten
Richard D. Moores wrote: > On Tue, Aug 28, 2012 at 1:21 AM, Timo wrote: >> Op 28-08-12 10:06, Richard D. Moores schreef: > >>> What if I wanted 3., 1234., etc. to be considered ints, as they are by >>> _validate_int() ? >> >> > isinstance(3., (int, float)) >> True >> >> Because 3. is a floa

Re: [Tutor] Why begin a function name with an underscore

2012-08-28 Thread Hugo Arts
On Tue, Aug 28, 2012 at 11:13 AM, Richard D. Moores wrote: > On Tue, Aug 28, 2012 at 1:21 AM, Timo wrote: > > Op 28-08-12 10:06, Richard D. Moores schreef: > > >> What if I wanted 3., 1234., etc. to be considered ints, as they are by > >> _validate_int() ? > > > > > isinstance(3., (int, flo

Re: [Tutor] Why begin a function name with an underscore

2012-08-28 Thread Richard D. Moores
On Tue, Aug 28, 2012 at 1:21 AM, Timo wrote: > Op 28-08-12 10:06, Richard D. Moores schreef: >> What if I wanted 3., 1234., etc. to be considered ints, as they are by >> _validate_int() ? > > isinstance(3., (int, float)) > True > > Because 3. is a float, not int. And >>> isinstance(3.7, (i

Re: [Tutor] Why begin a function name with an underscore

2012-08-28 Thread Mark Lawrence
On 28/08/2012 05:13, Richard D. Moores wrote: On Mon, Aug 27, 2012 at 6:33 PM, Japhy Bartlett wrote: something like: def _validate_int(obj): """Raise an exception if obj is not an integer.""" m = int(obj + 0) # May raise TypeError. if obj != m: raise ValueError('expec

Re: [Tutor] Why begin a function name with an underscore

2012-08-28 Thread Peter Otten
Timo wrote: > Op 28-08-12 10:06, Richard D. Moores schreef: >> On Tue, Aug 28, 2012 at 12:13 AM, Jerry Zhang >> wrote: >>> >>> 2012/8/28 Richard D. Moores >>> On Mon, Aug 27, 2012 at 6:33 PM, Japhy Bartlett wrote: > something like: > > def _validate_int(obj): >

Re: [Tutor] Why begin a function name with an underscore

2012-08-28 Thread Timo
Op 28-08-12 10:06, Richard D. Moores schreef: On Tue, Aug 28, 2012 at 12:13 AM, Jerry Zhang wrote: 2012/8/28 Richard D. Moores On Mon, Aug 27, 2012 at 6:33 PM, Japhy Bartlett wrote: something like: def _validate_int(obj): """Raise an exception if obj is not an integer.""" m =

Re: [Tutor] Why begin a function name with an underscore

2012-08-28 Thread Richard D. Moores
On Tue, Aug 28, 2012 at 12:13 AM, Jerry Zhang wrote: > > > 2012/8/28 Richard D. Moores > >> On Mon, Aug 27, 2012 at 6:33 PM, Japhy Bartlett >> wrote: >> >> > something like: >> > >> > def _validate_int(obj): >> > """Raise an exception if obj is not an integer.""" >> > m = int(obj + 0) #

Re: [Tutor] Why begin a function name with an underscore

2012-08-28 Thread Jerry Zhang
2012/8/28 Richard D. Moores > On Mon, Aug 27, 2012 at 6:33 PM, Japhy Bartlett > wrote: > > > something like: > > > > def _validate_int(obj): > > """Raise an exception if obj is not an integer.""" > > m = int(obj + 0) # May raise TypeError. > > if obj != m: > > raise ValueErr

Re: [Tutor] Why begin a function name with an underscore

2012-08-27 Thread Richard D. Moores
On Mon, Aug 27, 2012 at 6:33 PM, Japhy Bartlett wrote: > something like: > > def _validate_int(obj): > """Raise an exception if obj is not an integer.""" > m = int(obj + 0) # May raise TypeError. > if obj != m: > raise ValueError('expected an integer but got %r' % obj) > > >

Re: [Tutor] Why begin a function name with an underscore

2012-08-27 Thread 罗健忠
> > It's better to use the single-leading-underscore convention, _internal. > This isn't name mangled at all; it just indicates to others to "be careful > with this, it's an internal implementation detail; don't touch it if you > don't *fully* understand it". It's only a convention though. you ca