Re: Equivalent code to the bool() built-in function

2011-04-28 Thread Duncan Booth
Albert van der Horst wrote: >>I guess I never thought about it, but there isn't an 'xor' operator to >>go along with 'or' and 'and'. Must not be something I need very often. > > There is. <> applied to booleans is xor. > Best to get into the habit of using '!=' otherwise you'll find Python 3.x

Re: Equivalent code to the bool() built-in function

2011-04-28 Thread buck
I'm not not touching you! -- http://mail.python.org/mailman/listinfo/python-list

Re: Equivalent code to the bool() built-in function

2011-04-28 Thread Albert van der Horst
In article <9142usf51...@mid.individual.net>, Gregory Ewing wrote: >Chris Angelico wrote: > >> Remind me some day to finish work on my "ultimate programming >> language", which starts out with a clean slate and lets the programmer >> define his own operators and everything. > >Didn't someone alre

Re: Equivalent code to the bool() built-in function

2011-04-28 Thread Albert van der Horst
In article , Grant Edwards wrote: > >On Tue, Apr 19, 2011 at 7:09 AM, Christian Heimes wrote: >> Am 18.04.2011 21:58, schrieb John Nagle: >>> ?? ?? This is typical for languages which backed into a "bool" type, >>> rather than having one designed in. ??The usual result is a boolean >>> type with

Re: Equivalent code to the bool() built-in function

2011-04-19 Thread Gregory Ewing
Jean-Paul Calderone wrote: Also boolean xor is the same as !=. Only if you have booleans. Even without short circuiting, a boolean xor operator could provide the service of automatically booling things for you (is that a word?). Jean-Paul -- http://mail.python.org/mailman/listinfo/python-li

Re: Equivalent code to the bool() built-in function

2011-04-19 Thread Jean-Paul Calderone
On Apr 19, 10:23 am, Grant Edwards wrote: > On Tue, Apr 19, 2011 at 7:09 AM, Christian Heimes wrote: > > Am 18.04.2011 21:58, schrieb John Nagle: > >> ?? ?? This is typical for languages which backed into a "bool" type, > >> rather than having one designed in. ??The usual result is a boolean > >>

Re: Equivalent code to the bool() built-in function

2011-04-19 Thread Grant Edwards
On Tue, Apr 19, 2011 at 7:09 AM, Christian Heimes wrote: > Am 18.04.2011 21:58, schrieb John Nagle: >> ?? ?? This is typical for languages which backed into a "bool" type, >> rather than having one designed in. ??The usual result is a boolean >> type with numerical semantics, like >> >> ??>>> Tru

Re: Equivalent code to the bool() built-in function

2011-04-19 Thread Westley Martínez
On Tue, 2011-04-19 at 19:00 +1000, Chris Angelico wrote: > On Tue, Apr 19, 2011 at 6:43 PM, Steven D'Aprano > wrote: > > but I don't see how > > > > (arbitrary expression) + (another expression) + ... + (last expression) > > > > can have any guarantees applied. I mean, you can't even guarantee tha

Re: Equivalent code to the bool() built-in function

2011-04-19 Thread Chris Angelico
On Tue, Apr 19, 2011 at 6:43 PM, Steven D'Aprano wrote: > but I don't see how > > (arbitrary expression) + (another expression) + ... + (last expression) > > can have any guarantees applied. I mean, you can't even guarantee that > they won't raise an exception. Can you explain what you mean? What

Re: Equivalent code to the bool() built-in function

2011-04-19 Thread Steven D'Aprano
On Tue, 19 Apr 2011 16:26:50 +1000, Chris Angelico wrote: > On Tue, Apr 19, 2011 at 4:23 PM, Kushal Kumaran > wrote: >>> if a + b + c + d != 1: >>>    raise ValueError("Exactly one of a, b, c or d must be true.") >>> >>> >> Unless you're sure all of a, b, c, and d are boolean values, an int >> wi

Re: Equivalent code to the bool() built-in function

2011-04-18 Thread Chris Angelico
On Tue, Apr 19, 2011 at 4:23 PM, Kushal Kumaran wrote: >> if a + b + c + d != 1: >>    raise ValueError("Exactly one of a, b, c or d must be true.") >> > > Unless you're sure all of a, b, c, and d are boolean values, an int > with a negative value slipping in could result in the sum equaling 1, >

Re: Equivalent code to the bool() built-in function

2011-04-18 Thread Kushal Kumaran
On Tue, Apr 19, 2011 at 7:09 AM, Christian Heimes wrote: > Am 18.04.2011 21:58, schrieb John Nagle: >>     This is typical for languages which backed into a "bool" type, >> rather than having one designed in.  The usual result is a boolean >> type with numerical semantics, like >> >>  >>> True + T

Re: Equivalent code to the bool() built-in function

2011-04-18 Thread Christian Heimes
Am 18.04.2011 21:58, schrieb John Nagle: > This is typical for languages which backed into a "bool" type, > rather than having one designed in. The usual result is a boolean > type with numerical semantics, like > > >>> True + True > 2 I find the behavior rather useful. It allows multi-xor

Re: Equivalent code to the bool() built-in function

2011-04-18 Thread Gregory Ewing
Chris Angelico wrote: Remind me some day to finish work on my "ultimate programming language", which starts out with a clean slate and lets the programmer define his own operators and everything. Didn't someone already do that and call it "lisp"? :-) -- Greg -- http://mail.python.org/mailman/

Re: Equivalent code to the bool() built-in function

2011-04-18 Thread Gregory Ewing
John Nagle wrote: Pascal got this right. (A nice feature of Pascal was that "packed array of boolean" was a bit array). C, which originally lacked a "bool" type, got it wrong. So did Python. If Python had had a boolean type from the beginning, it probably wouldn't have been a subclass of in

Re: Equivalent code to the bool() built-in function

2011-04-18 Thread John Nagle
On 4/17/2011 5:12 PM, Gregory Ewing wrote: Chris Angelico wrote: Well, of course you can always implement bool as an int; Which Python used to do once upon a time -- and still does in a way, because bool is a subclass of int. The bool type was added mainly to provide a type that prints out a

Re: Equivalent code to the bool() built-in function

2011-04-18 Thread Duncan Booth
Steven D'Aprano wrote: > So in Python 2.2, Python introduced two new built-in names, True and > False, with values 1 and 0 respectively: > > [steve@sylar ~]$ python2.2 > Python 2.2.3 (#1, Aug 12 2010, 01:08:27) > [GCC 4.1.2 20070925 (Red Hat 4.1.2-27)] on linux2 > Type "help", "copyright", "cre

Re: Equivalent code to the bool() built-in function

2011-04-18 Thread candide
Le 18/04/2011 10:33, Raymond Hettinger a écrit : # -- def bool_equivalent(x): return True if x else False It's faster to write: def bool_equivalent(x): return not not x faster and ... smarter ;) -- http://mail.python.org/mailman/listinfo/pyth

Re: Equivalent code to the bool() built-in function

2011-04-18 Thread Raymond Hettinger
On Apr 16, 1:24 pm, candide wrote: > Consider the following code : > > # -- > def bool_equivalent(x): >      return True if x else False It's faster to write: def bool_equivalent(x): return not not x Raymond -- http://mail.python.org/mailman/listinfo/py

Re: Equivalent code to the bool() built-in function

2011-04-17 Thread Steven D'Aprano
On Sun, 17 Apr 2011 22:49:41 -0700, Chris Rebert wrote: >> Pro: You can do anything. >> Con: You can do anything. > > I think someone already beat you to it. They call their invention > "Lisp". :-P Also Forth. -- Steven -- http://mail.python.org/mailman/listinfo/python-list

Re: Equivalent code to the bool() built-in function

2011-04-17 Thread Chris Angelico
On Mon, Apr 18, 2011 at 3:49 PM, Chris Rebert wrote: >> Pro: You can do anything. >> Con: You can do anything. > > I think someone already beat you to it. They call their invention "Lisp". :-P Bah! Lisp comes, out of the box, with far too many features! No no no. If you want the + operator to add

Re: Equivalent code to the bool() built-in function

2011-04-17 Thread Chris Rebert
On Sun, Apr 17, 2011 at 9:53 PM, Chris Angelico wrote: > On Mon, Apr 18, 2011 at 2:40 PM, Ned Deily wrote: >> Even better: >> $ python2.7 -c 'False = True; print False' >> True > > http://bofh.ch/bofh/bofh13.html > >> Alas: >> $ python3 -c 'False = True; print(False)' >>  File "", line 1 >> Synt

Re: Equivalent code to the bool() built-in function

2011-04-17 Thread Chris Angelico
On Mon, Apr 18, 2011 at 2:40 PM, Ned Deily wrote: > Chris Angelico: >>  Dave Angel: >> bool = int >> Any language that allows you to do this is either awesome or >> terrifying. Come to think of it, there's not a lot of difference. > > Even better: > $ python2.7 -c 'False = True; print False'

Re: Equivalent code to the bool() built-in function

2011-04-17 Thread Ned Deily
Chris Angelico: > Dave Angel: > bool = int > Any language that allows you to do this is either awesome or > terrifying. Come to think of it, there's not a lot of difference. Even better: $ python2.7 -c 'False = True; print False' True Alas: $ python3 -c 'False = True; print(False)' File "

Re: Re: Equivalent code to the bool() built-in function

2011-04-17 Thread Daniel Kluev
On Mon, Apr 18, 2011 at 12:46 PM, Dave Angel wrote: > He didn't say that the function will call the bool() type (constructor), but > that it will use the bool type; Actually, he did say exactly that > Any boolean expression is going to be _calling the built-in ‘bool’ type > constructor_ (undersc

Re: Re: Equivalent code to the bool() built-in function

2011-04-17 Thread Chris Angelico
On Mon, Apr 18, 2011 at 11:46 AM, Dave Angel wrote: bool = int > Any language that allows you to do this is either awesome or terrifying. Come to think of it, there's not a lot of difference. Chris Angelico -- http://mail.python.org/mailman/listinfo/python-list

Re: Re: Equivalent code to the bool() built-in function

2011-04-17 Thread Dave Angel
On 01/-10/-28163 02:59 PM, Daniel Kluev wrote: On Sun, Apr 17, 2011 at 8:38 AM, Ben Finney wrote: It won't look up the *name* ‘bool’, but it will use that object. Any boolean expression is going to be calling the built-in ‘bool’ type constructor. So the answer to the OP's question is no: the f

Re: Equivalent code to the bool() built-in function

2011-04-17 Thread Chris Angelico
On Mon, Apr 18, 2011 at 10:22 AM, Steven D'Aprano wrote: types = [str, complex, float, bool] [f(x) for f, x in zip(types, (1, 2, 3, 4))] > ['1', (2+0j), 3.0, True] I believe this one would work fine with a function defined as per OP - zip takes callables, which could be types or functio

Re: Equivalent code to the bool() built-in function

2011-04-17 Thread Ben Finney
Daniel Kluev writes: > Actually, as I was curious myself, I've checked sources and found that > `True if x else False` will _not_ call bool(), it calls > PyObject_IsTrue() pretty much directly. Sure. By ‘bool(x)’ I'm referring only to the implementation inside that constructor. > So technically

Re: Equivalent code to the bool() built-in function

2011-04-17 Thread Steven D'Aprano
On Mon, 18 Apr 2011 01:22:59 +0200, candide wrote: > > What is the > > “shortcut” you refer to? > > bool(x) is nothing more than a shortcut for the following expression : > True if x else False. "Nothing more"? That's completely incorrect. bool is a type object, not an expression, so you can

Re: Equivalent code to the bool() built-in function

2011-04-17 Thread Gregory Ewing
Chris Angelico wrote: Well, of course you can always implement bool as an int; Which Python used to do once upon a time -- and still does in a way, because bool is a subclass of int. The bool type was added mainly to provide a type that prints out as 'True' or 'False' rather than 1 or 0. This

Re: Equivalent code to the bool() built-in function

2011-04-17 Thread Gregory Ewing
candide wrote: bool(x) is nothing more than a shortcut for the following expression : True if x else False. It's a much shorter and easier-to-read shortcut. Also keep in mind that if-else expressions are quite a recent addition to the language. Before that, we had 'not not x' as another equi

Re: Equivalent code to the bool() built-in function

2011-04-17 Thread Ben Finney
candide writes: > Le 17/04/2011 11:46, Ben Finney a écrit : > > What is the “shortcut” you refer to? > > bool(x) is nothing more than a shortcut for the following expression : > True if x else False. We're going around in circles. I've already pointed out that ‘bool(x)’ is what the expression yo

Re: Equivalent code to the bool() built-in function

2011-04-17 Thread Daniel Kluev
On Sun, Apr 17, 2011 at 8:38 AM, Ben Finney wrote: > It won't look up the *name* ‘bool’, but it will use that object. Any > boolean expression is going to be calling the built-in ‘bool’ type > constructor. > > So the answer to the OP's question is no: the function isn't equivalent > to the type, b

Re: Equivalent code to the bool() built-in function

2011-04-17 Thread candide
Le 17/04/2011 11:46, Ben Finney a écrit : > candide writes: > >> First because I was doubting the true interest of the bool() type. In >> fact, it appears that it's _semantically_ a shortcut for >> True if x else False. > > That bends my brain. Both ‘True’ and ‘False’ are instances of the ‘bool’

Re: Equivalent code to the bool() built-in function

2011-04-17 Thread Daniel Kluev
On Sun, Apr 17, 2011 at 7:38 PM, candide wrote: > I could't imagine a builtin function having a so trivial implementation. As it was pointed out, its not function, its type, SETBUILTIN("bool", &PyBool_Type); While its __new__ is indeed trivial (in essence, it just calls PyObject

Re: Equivalent code to the bool() built-in function

2011-04-17 Thread Ben Finney
candide writes: > First because I was doubting the true interest of the bool() type. In > fact, it appears that it's _semantically_ a shortcut for > True if x else False. That bends my brain. Both ‘True’ and ‘False’ are instances of the ‘bool’ type. So of course the ‘bool’ constructor will retur

Re: Equivalent code to the bool() built-in function

2011-04-17 Thread Chris Angelico
On Sun, Apr 17, 2011 at 6:38 PM, candide wrote: > I also try to consider how essential the bool() type is. > Again compare with int() or str() types. Well, of course you can always implement bool as an int; C has done this for decades, and it hasn't killed it. You can also implement integers as s

Re: Equivalent code to the bool() built-in function

2011-04-17 Thread candide
Le 17/04/2011 04:39, Ben Finney a écrit : Why do you need to know? (I should have asked that question earlier.) First because I was doubting the true interest of the bool() type. In fact, it appears that it's _semantically_ a shortcut for True if x else False. I could't imagine a builtin f

Re: Equivalent code to the bool() built-in function

2011-04-16 Thread Ben Finney
candide writes: > Le 16/04/2011 23:13, Ben Finney a écrit : > > > The ‘bool’ built-in is not a function. > > Oops, unfortunate confusion!! but built-in types and built-in > functions are sometimes so similar from the user's point of view ;) Yes, intentionally so, because: > All the same, you ca

Re: Equivalent code to the bool() built-in function

2011-04-16 Thread Chris Rebert
On Sat, Apr 16, 2011 at 4:51 PM, candide wrote: > Le 16/04/2011 23:38, Ben Finney a écrit : > >> So the answer to the OP's question is no: the function isn't equivalent >> to the type, > > Can bool() type and bool_equivalent() function return different values ? No. The distinction being drawn is

Re: Equivalent code to the bool() built-in function

2011-04-16 Thread candide
Le 16/04/2011 23:13, Ben Finney a écrit : The ‘bool’ built-in is not a function. >>> type(bool) Oops, unfortunate confusion!! but built-in types and built-in functions are sometimes so similar from the user's point of view ;) All the same, you can notice that the official doc

Re: Equivalent code to the bool() built-in function

2011-04-16 Thread candide
Le 16/04/2011 23:38, Ben Finney a écrit : So the answer to the OP's question is no: the function isn't equivalent to the type, Can bool() type and bool_equivalent() function return different values ? because the OP's ‘bool_equivalent’ function necessarily uses the built-in ‘bool’ type, whi

Re: Equivalent code to the bool() built-in function

2011-04-16 Thread Ben Finney
Chris Rebert writes: > That is, `True if x else False` conceptually gets compiled down to > `True if bool(x) == 1 else False` (but without doing a run-time lookup > of "bool"). It won't look up the *name* ‘bool’, but it will use that object. Any boolean expression is going to be calling the buil

Re: Equivalent code to the bool() built-in function

2011-04-16 Thread Ben Finney
candide writes: > Is the bool_equivalent() function really equivalent to the bool() > built-in function ? The ‘bool’ built-in is not a function. >>> type(bool) -- \ “Generally speaking, the errors in religion are dangerous; | `\those in philosophy only ridiculous.” —D

Re: Equivalent code to the bool() built-in function

2011-04-16 Thread Chris Rebert
On Sat, Apr 16, 2011 at 1:24 PM, candide wrote: > Consider the following code : > > # -- > def bool_equivalent(x): >    return True if x else False > > > # testing ... > > def foo(x): >    return 10*x > > class C: >    pass > > for x in [42, ("my","baby"), "baob

Equivalent code to the bool() built-in function

2011-04-16 Thread candide
Consider the following code : # -- def bool_equivalent(x): return True if x else False # testing ... def foo(x): return 10*x class C: pass for x in [42, ("my","baby"), "baobab", max, foo, C] + [None, 0, "", [], {},()]: print bool(x)==bool