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

2011-04-28 Thread Albert van der Horst
In article iok5tg$svv$1...@reader1.panix.com, Grant Edwards invalid@invalid.invalid wrote: On Tue, Apr 19, 2011 at 7:09 AM, Christian Heimes li...@cheimes.de wrote: Am 18.04.2011 21:58, schrieb John Nagle: ?? ?? This is typical for languages which backed into a bool type, rather than having

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 greg.ew...@canterbury.ac.nz 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.

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 Duncan Booth
Albert van der Horst alb...@spenarnc.xs4all.nl 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

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

2011-04-19 Thread Kushal Kumaran
On Tue, Apr 19, 2011 at 7:09 AM, Christian Heimes li...@cheimes.de 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 +

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

2011-04-19 Thread Chris Angelico
On Tue, Apr 19, 2011 at 4:23 PM, Kushal Kumaran kushal.kumaran+pyt...@gmail.com 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

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 kushal.kumaran+pyt...@gmail.com 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

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 steve+comp.lang.pyt...@pearwood.info 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

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 steve+comp.lang.pyt...@pearwood.info wrote: but I don't see how (arbitrary expression) + (another expression) + ... + (last expression) can have any guarantees applied. I mean, you

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 li...@cheimes.de 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 ??

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

2011-04-19 Thread Jean-Paul Calderone
On Apr 19, 10:23 am, Grant Edwards inva...@invalid.invalid wrote: On Tue, Apr 19, 2011 at 7:09 AM, Christian Heimes li...@cheimes.de 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

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 --

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

2011-04-18 Thread Chris Angelico
On Mon, Apr 18, 2011 at 3:49 PM, Chris Rebert c...@rebertia.com 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 +

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

2011-04-18 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-18 Thread Raymond Hettinger
On Apr 16, 1:24 pm, candide cand...@free.invalid 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 --

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 ;) --

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

2011-04-18 Thread Duncan Booth
Steven D'Aprano steve+comp.lang.pyt...@pearwood.info 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

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

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 int

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 --

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 tests

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

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 candide@free.invalid 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

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

2011-04-17 Thread Ben Finney
candide candide@free.invalid 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’

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 candide@free.invalid 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

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

2011-04-17 Thread candide
Le 17/04/2011 11:46, Ben Finney a écrit : candidecandide@free.invalid 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

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 ben+pyt...@benfinney.id.au 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

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

2011-04-17 Thread Ben Finney
candide candide@free.invalid 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

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

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.

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 do

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

2011-04-17 Thread Ben Finney
Daniel Kluev dan.kl...@gmail.com 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.

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 steve+comp.lang.pyt...@pearwood.info 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,

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 Finneyben+pyt...@benfinney.id.au 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

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 da...@ieee.org 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 Daniel Kluev
On Mon, Apr 18, 2011 at 12:46 PM, Dave Angel da...@ieee.org 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

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 string,

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 n...@acm.org 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 Chris Rebert
On Sun, Apr 17, 2011 at 9:53 PM, Chris Angelico ros...@gmail.com wrote: On Mon, Apr 18, 2011 at 2:40 PM, Ned Deily n...@acm.org wrote: snip Even better: $ python2.7 -c 'False = True; print False' True http://bofh.ch/bofh/bofh13.html Alas: $ python3 -c 'False = True; print(False)'  File

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

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 candide@free.invalid 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),

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

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

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

2011-04-16 Thread Ben Finney
Chris Rebert c...@rebertia.com 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

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,

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) type 'type' 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

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 candide@free.invalid 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

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

2011-04-16 Thread Ben Finney
candide candide@free.invalid 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