Re: Why bool( object )?

2009-05-02 Thread Arnaud Delobelle
Steven D'Aprano writes: > On Fri, 01 May 2009 15:03:30 -0700, Aaron Brady wrote: > >> On May 1, 4:30 am, Steven D'Aprano > cybersource.com.au> wrote: >>> On Fri, 01 May 2009 16:30:19 +1200, Lawrence D'Oliveiro wrote: >>> > I have never written anything so unbelievable in my life. And I hope >>> >

Re: Why bool( object )?

2009-05-01 Thread Steven D'Aprano
On Fri, 01 May 2009 15:03:30 -0700, Aaron Brady wrote: > On May 1, 4:30 am, Steven D'Aprano cybersource.com.au> wrote: >> On Fri, 01 May 2009 16:30:19 +1200, Lawrence D'Oliveiro wrote: >> > I have never written anything so unbelievable in my life. And I hope >> > I never will. >> >> I didn't say

Re: Why bool( object )?

2009-05-01 Thread Steven D'Aprano
On Fri, 01 May 2009 16:30:19 +1200, Lawrence D'Oliveiro wrote: > I have never written anything so unbelievable in my life. And I hope I > never will. I didn't say you did. If anyone thought I was quoting Lawrence's code, I'd be surprised. It was not my intention to put words into your mouth. Bu

Re: Why bool( object )?

2009-05-01 Thread Paul Rubin
Aaron Brady writes: > I think you are looking at an 'ireduce' function, which doesn't > exist in 'itertools' yet. Nothing is being done with the return value. sum(1 for x in imap(func, seq)) is enough to force evaluation of func on each element of seq. -- http://mail.python.org/mailman/listi

Re: Why bool( object )?

2009-05-01 Thread Aaron Brady
On May 1, 4:30 am, Steven D'Aprano wrote: > On Fri, 01 May 2009 16:30:19 +1200, Lawrence D'Oliveiro wrote: > > I have never written anything so unbelievable in my life. And I hope I > > never will. > > I didn't say you did. If anyone thought I was quoting Lawrence's code, > I'd be surprised. It wa

Re: Why bool( object )?

2009-05-01 Thread Steven D'Aprano
On Fri, 01 May 2009 01:56:50 -0700, Paul Rubin wrote: > Steven D'Aprano writes: >> (2) Why assume that a, b and c are sequences with a fast __len__ >> method? They might be (say) linked lists that take O(N) to calculate >> the length, or binary trees that don't even have a length, but can be >> i

Re: Why bool( object )?

2009-05-01 Thread Hendrik van Rooyen
"Paul Rubin" wrote: > Steven D'Aprano writes: > > for x in a or b or c: > > do_something_with(x) > > Ugh > > for x in [a,b,c]: > if len(x) > 0: >do_something_with(x) >break > Ugh for x in [a,b,c]: if x: do_something_wi

Re: Why bool( object )?

2009-05-01 Thread Paul Rubin
Steven D'Aprano writes: > (2) Why assume that a, b and c are sequences with a fast __len__ method? > They might be (say) linked lists that take O(N) to calculate the length, > or binary trees that don't even have a length, but can be iterated over. Why assume they have a bool method? Or a __or

Re: Why bool( object )?

2009-05-01 Thread Steven D'Aprano
On Fri, 01 May 2009 00:22:22 -0700, Paul Rubin wrote: > Steven D'Aprano writes: >> for x in a or b or c: >> do_something_with(x) > > Ugh > > for x in [a,b,c]: > if len(x) > 0: >do_something_with(x) >break What ugly, wasteful code. And it's *wrong* -- it doesn't d

Re: Why bool( object )?

2009-05-01 Thread Paul Rubin
Steven D'Aprano writes: > for x in a or b or c: > do_something_with(x) Ugh for x in [a,b,c]: if len(x) > 0: do_something_with(x) break -- http://mail.python.org/mailman/listinfo/python-list

Re: Why bool( object )?

2009-04-30 Thread Lawrence D'Oliveiro
In message , Steven D'Aprano wrote: > The reason why Lawrence's insistence is so badly wrong becomes more > apparent if you look at what you can do with boolean contexts other than > simple `if` tests. Compare: > > > for x in a or b or c: > do_something_with(x) > > > versus: > > > if le

Re: Why bool( object )?

2009-04-30 Thread JanC
Steven D'Aprano wrote: > There are 4,294,967,296 integers that can be represented in 32 bits. Only > one of them represents zero. Or you can even define it as not including zero... ;) -- JanC -- http://mail.python.org/mailman/listinfo/python-list

Re: Why bool( object )?

2009-04-29 Thread r
On Apr 28, 7:22 am, "Colin J. Williams" wrote: > Lie Ryan wrote: > > I'm puzzled by the last sentence: > > *** Python 2.6.2 (r262:71605, Apr 14 > 2009, 22:40:02) [MSC v.1500 32 bit > (Intel)] on win32. *** > > >>> bool(0) > False > >>> bool(-1) > True > >>> bool(-100) > True > > Colin W. What's w

Re: Why bool( object )?

2009-04-29 Thread Steven D'Aprano
On Wed, 29 Apr 2009 17:35:28 +0200, Marco Mariani wrote: > Bruno Desthuilliers wrote: > >> Lawrence D'Oliveiro a écrit : > What is the rationale for considering all instances true of a user- defined type? >>> >>> It's a stupid idea, >> >> Nope, it's a very sensible default (given you

Re: Why bool( object )?

2009-04-29 Thread Aaron Brady
On Apr 28, 9:54 pm, Steven D'Aprano wrote: > On Tue, 28 Apr 2009 11:59:18 -0700, Aaron Brady wrote: > >> To steal an idiom from Laura: Python has a float-shaped Nothing 0.0, a > >> list-shaped Nothing [], a dict-shaped Nothing {}, an int-shaped Nothing > >> 0, a singleton Nothing None, and so fort

Re: Why bool( object )?

2009-04-29 Thread Marco Mariani
Bruno Desthuilliers wrote: Lawrence D'Oliveiro a écrit : What is the rationale for considering all instances true of a user- defined type? It's a stupid idea, Nope, it's a very sensible default (given you can redefine the 'nothingness' value of your types instances), specially when the

Re: Why bool( object )?

2009-04-29 Thread Aahz
In article <340175e7-b349-4ca2-bf66-fa9113253...@v23g2000pro.googlegroups.com>, Aaron Brady wrote: > >The sound of that metaphor is rather pleasing ('sweet nothings'), but >I'm not so sure that metaphors belong in computer science and >programming. Well, you won't be a good programmer if you c

Re: Why bool( object )?

2009-04-29 Thread Bruno Desthuilliers
Lawrence D'Oliveiro a écrit : In message <54cb7f8a- fef4-4bf8-8054-16dc9b5c8...@d2g2000pra.googlegroups.com>, Aaron Brady wrote: What is the rationale for considering all instances true of a user- defined type? It's a stupid idea, Nope, it's a very sensible default (given you can redefine

Re: Why bool( object )?

2009-04-28 Thread Steven D'Aprano
On Tue, 28 Apr 2009 11:59:18 -0700, Aaron Brady wrote: >> To steal an idiom from Laura: Python has a float-shaped Nothing 0.0, a >> list-shaped Nothing [], a dict-shaped Nothing {}, an int-shaped Nothing >> 0, a singleton Nothing None, and so forth. > > The sound of that metaphor is rather pleasi

Re: Why bool( object )?

2009-04-28 Thread Rhodri James
On Tue, 28 Apr 2009 19:59:18 +0100, Aaron Brady wrote: The sound of that metaphor is rather pleasing ('sweet nothings'), but I'm not so sure that metaphors belong in computer science and programming. Nothing can't have many shapes. Having no onions is the same as having no carrots. Not if

Re: Why bool( object )?

2009-04-28 Thread Aaron Brady
On Apr 28, 2:39 am, Steven D'Aprano wrote: > On Mon, 27 Apr 2009 23:11:11 -0700, Aaron Brady wrote: > > What is the rationale for considering all instances true of a user- > > defined type?  Is it strictly a practical stipulation, or is there > > something conceptually true about objects? > > Seve

Re: Why bool( object )?

2009-04-28 Thread Scott David Daniels
Steven D'Aprano wrote: On Mon, 27 Apr 2009 23:11:11 -0700, Aaron Brady wrote: ... In a boolean (or truth) context, Something and Nothing behave like True > and False in languages with real booleans: if obj: print "I am Something" else: print "I am Nothing" If you define the short-circ

Re: Why bool( object )?

2009-04-28 Thread Gabriel Genellina
En Tue, 28 Apr 2009 09:22:01 -0300, Colin J. Williams escribió: Lie Ryan wrote: Aaron Brady wrote: This makes it so all objects except False, None, 0, and empty containers are true by default. I am not convinced that 'if ' should have any meaning; it should probably throw an exception. Is

Re: Why bool( object )?

2009-04-28 Thread Andre Engels
On Tue, Apr 28, 2009 at 2:22 PM, Colin J. Williams wrote: > Lie Ryan wrote: >> >> Aaron Brady wrote: >>> >>> What is the rationale for considering all instances true of a user- >>> defined type? >> >> User-defined objects (or type) can override .__len__() [usually container >> types] or .__nonzero

Re: Why bool( object )?

2009-04-28 Thread Colin J. Williams
Lie Ryan wrote: Aaron Brady wrote: What is the rationale for considering all instances true of a user- defined type? User-defined objects (or type) can override .__len__() [usually container types] or .__nonzero__() to make bool() returns False. Is it strictly a practical stipulation, or

Re: Why bool( object )?

2009-04-28 Thread Lawrence D'Oliveiro
In message <54cb7f8a- fef4-4bf8-8054-16dc9b5c8...@d2g2000pra.googlegroups.com>, Aaron Brady wrote: > What is the rationale for considering all instances true of a user- > defined type? It's a stupid idea, and there seem to be instances of users tripping over it here in comp.lang.python every wee

Re: Why bool( object )?

2009-04-28 Thread Steven D'Aprano
On Mon, 27 Apr 2009 23:11:11 -0700, Aaron Brady wrote: > What is the rationale for considering all instances true of a user- > defined type? Is it strictly a practical stipulation, or is there > something conceptually true about objects? Seven years ago, in an attempt to convince Guido *not* to

Re: Why bool( object )?

2009-04-28 Thread Aaron Brady
On Apr 28, 1:35 am, Lie Ryan wrote: > Aaron Brady wrote: > > What is the rationale for considering all instances true of a user- > > defined type?   > > User-defined objects (or type) can override .__len__() [usually > container types] or .__nonzero__() to make bool() returns False. > > > Is it st

Re: Why bool( object )?

2009-04-27 Thread Chris Rebert
On Mon, Apr 27, 2009 at 11:11 PM, Aaron Brady wrote: > What is the rationale for considering all instances true of a user- > defined type?  Is it strictly a practical stipulation, or is there > something conceptually true about objects? > > ''' > object.__bool__(self) > If a class defines neither

Re: Why bool( object )?

2009-04-27 Thread Lie Ryan
Aaron Brady wrote: What is the rationale for considering all instances true of a user- defined type? User-defined objects (or type) can override .__len__() [usually container types] or .__nonzero__() to make bool() returns False. Is it strictly a practical stipulation, or is there somethi

Re: Why bool( object )?

2009-04-27 Thread Stephen Hansen
On Mon, Apr 27, 2009 at 11:11 PM, Aaron Brady wrote: > What is the rationale for considering all instances true of a user- > defined type? Is it strictly a practical stipulation, or is there > something conceptually true about objects? > > ''' > object.__bool__(self) > If a class defines neither

Why bool( object )?

2009-04-27 Thread Aaron Brady
What is the rationale for considering all instances true of a user- defined type? Is it strictly a practical stipulation, or is there something conceptually true about objects? ''' object.__bool__(self) If a class defines neither __len__() nor __bool__(), all its instances are considered true. ''