Velocity Reviews > Argument of the bool function

# Argument of the bool function

candide
Guest
Posts: n/a

 04-08-2011
About the standard function bool(), Python's official documentation
tells us the following :

bool([x])
Convert a value to a Boolean, using the standard truth testing procedure.

In this context, what exactly a "value" is referring to ?

For instance,

>>> x=42
>>> bool(x=5)

True
>>>

but _expression_ :

x=42

has no value.

Benjamin Kaplan
Guest
Posts: n/a

 04-08-2011
On Fri, Apr 8, 2011 at 12:26 PM, candide <(E-Mail Removed)> wrote:
> About the standard function bool(), Python's official documentation tells us
> the following :
>
> bool([x])
> Convert a value to a Boolean, using the standard truth testing procedure.
>
>
> In this context, what exactly a "value" is referring to ?
>
>
> For instance,
>
>
>>>> x=42
>>>> bool(x=5)

> True
>>>>

>
>
> but _expression_ :
>
> x=42
>
>
> has no value.
>

That's because bool(x=5) isn't doing what you think.

>>> bool(y=5)

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'y' is an invalid keyword argument for this function

bool(x=5) is just passing the value 5 as the argument "x" to the function.

"value" means just what you'd think- any constant or any value that's
been assigned to.

>
>
>
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>

Mel
Guest
Posts: n/a

 04-08-2011
candide wrote:
> About the standard function bool(), Python's official documentation
> tells us the following :
>
> bool([x])
> Convert a value to a Boolean, using the standard truth testing procedure.
>
> In this context, what exactly a "value" is referring to ?
>
> For instance,
> >>> x=42
> >>> bool(x=5)

> True
> >>>

Cute. What's happening here is that `x=5` isn't really an expression.
It's passing a value to the named parameter `x`, specified in the
definition of `bool`. Try it with something else:

Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
[GCC 4.4.3] on linux2
>>> bool(y=5)

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'y' is an invalid keyword argument for this function

Mel.

Ian Kelly
Guest
Posts: n/a

 04-08-2011
On Fri, Apr 8, 2011 at 10:26 AM, candide <(E-Mail Removed)> wrote:
>>>> x=42
>>>> bool(x=5)

> True
>>>>

>
>
> but _expression_ :
>
> x=42
>
>
> has no value.

"x=42" is an assignment statement, not an expression.
In "bool(x=5)", "x=5" is also not an expression. It's passing the
expression "5" in as the parameter x, using a keyword argument.

candide
Guest
Posts: n/a

 04-08-2011
Le 08/04/2011 18:43, Ian Kelly a écrit :

> "x=42" is an assignment statement, not an expression.

Right, I was confounding with C

In fact, respect to this question, the documentation makes things
unambiguous :

-----------------
In contrast to many other languages, not all language constructs are
expressions. There are also statements which cannot be used as
expressions, such as print or if. Assignments are also statements, not
expressions.
-----------------

> In "bool(x=5)", "x=5" is also not an expression. It's passing the
> expression "5" in as the parameter x, using a keyword argument.

You are probably right but how do you deduce this brilliant
interpretation from the wording given in the documentation ?

Ethan Furman
Guest
Posts: n/a

 04-08-2011
candide wrote:
> Le 08/04/2011 18:43, Ian Kelly a écrit :
>> In "bool(x=5)", "x=5" is also not an expression. It's passing the
>> expression "5" in as the parameter x, using a keyword argument.
>>

> You are probably right but how do you deduce this brilliant
> interpretation from the wording given in the documentation ?

Look at your original post, which contains the excerpt from the docs
that you put there:
>
> bool([x])
> Convert a value to a Boolean, using the standard truth testing
> procedure.
>

As you can see, the parameter name is 'x'.

~Ethan~

candide
Guest
Posts: n/a

 04-08-2011
Le 09/04/2011 00:03, Ethan Furman a écrit :

> > bool([x])
> > Convert a value to a Boolean, using the standard truth testing
> > procedure.
> >

>
> As you can see, the parameter name is 'x'.

OK, your response is clarifying my point

I didn't realize that in the bool([x]) syntax, identifier x refers to a
"genuine" argument [I was considering x as referring to a "generic"
object having a boolean value].

Nevertheless, compare with the definition the doc provides for the
builtin function dir():

dir([object])
[definition omited, just observe the declaration syntax]

Now, lets make a try

>>> dir(object="Explicit is better than implicit")

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: dir() takes no keyword arguments
>>>

Not very meaningful, isn't it ?

Lie Ryan
Guest
Posts: n/a

 04-09-2011
On 04/09/11 08:59, candide wrote:
> Le 09/04/2011 00:03, Ethan Furman a écrit :
>
>> > bool([x])
>> > Convert a value to a Boolean, using the standard truth testing
>> > procedure.
>> >

>>
>> As you can see, the parameter name is 'x'.

>
>
> OK, your response is clarifying my point
>
>
> I didn't realize that in the bool([x]) syntax, identifier x refers to a
> "genuine" argument [I was considering x as referring to a "generic"
> object having a boolean value].
>
>
> Nevertheless, compare with the definition the doc provides for the
> builtin function dir():
>
> dir([object])
> [definition omited, just observe the declaration syntax]
>
> Now, lets make a try
>
>>>> dir(object="Explicit is better than implicit")

> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> TypeError: dir() takes no keyword arguments
>>>>

>
> Not very meaningful, isn't it ?

The error says it unambiguously, dir() does not take *keyword*
arguments; instead dir() takes *positional* argument:

dir("Explicit is better than implicit")

Robert Kern
Guest
Posts: n/a

 04-09-2011
On 2011-04-08 17:59 , candide wrote:
> Le 09/04/2011 00:03, Ethan Furman a écrit :
>
>> > bool([x])
>> > Convert a value to a Boolean, using the standard truth testing
>> > procedure.
>> >

>>
>> As you can see, the parameter name is 'x'.

>
>
> OK, your response is clarifying my point
>
>
> I didn't realize that in the bool([x]) syntax, identifier x refers to a
> "genuine" argument [I was considering x as referring to a "generic" object
> having a boolean value].
>
>
> Nevertheless, compare with the definition the doc provides for the builtin
> function dir():
>
> dir([object])
> [definition omited, just observe the declaration syntax]
>
> Now, lets make a try
>
> >>> dir(object="Explicit is better than implicit")

> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> TypeError: dir() takes no keyword arguments
> >>>

>
> Not very meaningful, isn't it ?

No one is saying that every instance of "foo([arg])" in the docs means that the
given argument is named such that it is available for keyword arguments. What
people are saying is that for bool(), *that happens to be the case*.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
an underlying truth."
-- Umberto Eco

candide
Guest
Posts: n/a

 04-10-2011
Le 10/04/2011 01:22, Robert Kern a écrit :

> No one is saying that every instance of "foo([arg])" in the docs means
> that the given argument is named such that it is available for keyword
> arguments. What people are saying is that for bool(), *that happens to
> be the case*.
>

what a piece of luck!