Argument of the bool function

Discussion in 'Python' started by candide, Apr 8, 2011.

  1. candide

    candide Guest

    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.
     
    candide, Apr 8, 2011
    #1
    1. Advertising

  2. On Fri, Apr 8, 2011 at 12:26 PM, 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
    >>>>

    >
    >
    > 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
    >
     
    Benjamin Kaplan, Apr 8, 2011
    #2
    1. Advertising

  3. candide

    Mel Guest

    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
    Type "help", "copyright", "credits" or "license" for more information.
    >>> 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.
     
    Mel, Apr 8, 2011
    #3
  4. candide

    Ian Kelly Guest

    On Fri, Apr 8, 2011 at 10:26 AM, candide <> 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.
     
    Ian Kelly, Apr 8, 2011
    #4
  5. candide

    candide Guest

    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 ?
     
    candide, Apr 8, 2011
    #5
  6. candide

    Ethan Furman Guest

    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~
     
    Ethan Furman, Apr 8, 2011
    #6
  7. candide

    candide Guest

    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 ?
     
    candide, Apr 8, 2011
    #7
  8. candide

    Lie Ryan Guest

    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")
     
    Lie Ryan, Apr 9, 2011
    #8
  9. candide

    Robert Kern Guest

    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
    that is made terrible by our own mad attempt to interpret it as though it had
    an underlying truth."
    -- Umberto Eco
     
    Robert Kern, Apr 10, 2011
    #9
  10. candide

    candide Guest

    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! ;)
     
    candide, Apr 10, 2011
    #10
  11. On 2011-04-09, Lie Ryan <> wrote:
    > On 04/09/11 08:59, candide wrote:
    >> Le 09/04/2011 00:03, Ethan Furman a ?crit :
    >>
    >>> > bool([x])


    >> dir([object])


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


    I think the point is that both cases are documented exactly the same.

    --
    Grant
     
    Grant Edwards, Apr 10, 2011
    #11
  12. candide

    rusi Guest

    On Apr 10, 8:35 am, Grant Edwards <> wrote:
    > On 2011-04-09, Lie Ryan <> wrote:
    >
    > > On 04/09/11 08:59, candide wrote:
    > >> Le 09/04/2011 00:03, Ethan Furman a ?crit :

    >
    > >>>  > bool([x])
    > >> dir([object])
    > >> 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")

    >
    > I think the point is that both cases are documented exactly the same.


    In what case(s) would a keyword arg to bool be reasonable?
     
    rusi, Apr 10, 2011
    #12
  13. candide

    Robert Kern Guest

    On 2011-04-09 23:15 , rusi wrote:
    > On Apr 10, 8:35 am, Grant Edwards<> wrote:
    >> On 2011-04-09, Lie Ryan<> wrote:
    >>
    >>> On 04/09/11 08:59, candide wrote:
    >>>> Le 09/04/2011 00:03, Ethan Furman a ?crit :

    >>
    >>>>> > bool([x])
    >>>> dir([object])
    >>>> 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")

    >>
    >> I think the point is that both cases are documented exactly the same.

    >
    > In what case(s) would a keyword arg to bool be reasonable?


    It's just an implementation detail. It's not worth the electrons wasted in this
    thread already.

    --
    Robert Kern

    "I have come to believe that the whole world is an enigma, a harmless enigma
    that is made terrible by our own mad attempt to interpret it as though it had
    an underlying truth."
    -- Umberto Eco
     
    Robert Kern, Apr 10, 2011
    #13
  14. candide

    candide Guest

    Le 08/04/2011 18:41, Benjamin Kaplan a écrit :

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



    Anyway, passing x as a keyword argument to the bool function appears to
    be very rare : i did a regexp search for about 30000 source-code Python
    files (among them official Python source-code, Django, Sphinx, Eric
    source-code and many more sources of valuable Python code) and I didn't
    find even one.
     
    candide, Apr 10, 2011
    #14
  15. On Sun, Apr 10, 2011 at 10:54 PM, candide <> wrote:
    > Anyway, passing x as a keyword argument to the bool function appears to be
    > very rare : i did a regexp search for about 30000 source-code Python files
    > (among them official Python source-code, Django, Sphinx, Eric source-code
    > and many more sources of valuable Python code) and I didn't find even one.


    Who would use keyword arguments with a function that takes only one arg anyway?

    ChrisA
     
    Chris Angelico, Apr 10, 2011
    #15
  16. candide

    Mel Guest

    Chris Angelico wrote:

    > Who would use keyword arguments with a function that takes only one arg
    > anyway?


    It's hard to imagine. Maybe somebody trying to generalize function calls
    (trying to interpret some other language using a python program?)

    # e.g. input winds up having the effect of ..
    function = bool
    name = 'x'
    value = 'the well at the end of the world'
    ## ...
    actions.append ((function, {name:value}))
    ## ...
    for function, args in actions:
    results.append (function (**args))

    Not something I, for one, do every day. But regularity in a language is
    good when you can get it, especially for abstract things like that.

    I can sort of guess that `dir` was perhaps coded in C for speed and doesn't
    spend time looking for complicated argument lists.

    Python is a pragmatic language, so all the rules come pre-broken.


    Mel.
     
    Mel, Apr 10, 2011
    #16
  17. On 10-Apr-11 12:21 PM, Mel wrote:
    > Chris Angelico wrote:
    >
    >> Who would use keyword arguments with a function that takes only one arg
    >> anyway?

    >
    > It's hard to imagine. Maybe somebody trying to generalize function calls
    > (trying to interpret some other language using a python program?)
    >
    > # e.g. input winds up having the effect of ..
    > function = bool
    > name = 'x'
    > value = 'the well at the end of the world'
    > ## ...
    > actions.append ((function, {name:value}))
    > ## ...
    > for function, args in actions:
    > results.append (function (**args))
    >
    > Not something I, for one, do every day. But regularity in a language is
    > good when you can get it, especially for abstract things like that.
    >
    > I can sort of guess that `dir` was perhaps coded in C for speed and doesn't
    > spend time looking for complicated argument lists.
    >
    > Python is a pragmatic language, so all the rules come pre-broken.
    >
    >
    > Mel.

    This thread has lasted 3 days so far.

    I presume that it is agreed they the following is a satisfactory outcome:

    *** Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit
    (Intel)] on win32. ***
    >>> bool(x=0)

    False
    >>> bool(x=1)

    True
    >>>


    Colin W.
     
    Colin J. Williams, Apr 10, 2011
    #17
  18. candide

    Ethan Furman Guest

    Mel wrote:
    > Python is a pragmatic language, so all the rules come pre-broken.


    +1 QOTW
     
    Ethan Furman, Apr 12, 2011
    #18
  19. Am 10.04.2011 18:21, schrieb Mel:
    > Chris Angelico wrote:
    >
    >> Who would use keyword arguments with a function that takes only one arg
    >> anyway?

    >
    > It's hard to imagine. Maybe somebody trying to generalize function calls
    > (trying to interpret some other language using a python program?)
    >
    > # e.g. input winds up having the effect of ..
    > function = bool
    > name = 'x'
    > value = 'the well at the end of the world'
    > ## ...
    > actions.append ((function, {name:value}))
    > ## ...
    > for function, args in actions:
    > results.append (function (**args))


    Wrong structure.

    Better do

    function = bool
    value = 'the well at the end of the world'
    ## ...
    actions.append((function, (value,), {}))
    ## ...
    for function, args, kwargs in actions:
    results.append(function(*args, **kwargs))

    or maybe even better (taking care for closures):

    function = bool
    value = 'the well at the end of the world'
    ## ...
    actions.append(lambda val=value: function(val))
    ## ...
    for function in actions:
    results.append(function())


    >
    > Not something I, for one, do every day. But regularity in a language is
    > good when you can get it, especially for abstract things like that.
    >
    > I can sort of guess that `dir` was perhaps coded in C for speed and doesn't
    > spend time looking for complicated argument lists.
    >
    > Python is a pragmatic language, so all the rules come pre-broken.
    >
    >
    > Mel.
     
    Thomas Rachel, Apr 25, 2011
    #19
  20. On Tue, Apr 26, 2011 at 12:29 AM, Thomas Rachel
    <>
    wrote:
    > for function in actions:
    >     results.append(function())


    Can this become:

    results = [function() for function in actions]

    Chris Angelico
     
    Chris Angelico, Apr 25, 2011
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Kial
    Replies:
    1
    Views:
    726
    Eric Enright
    Nov 15, 2004
  2. S?ren Gammelmark
    Replies:
    1
    Views:
    1,989
    Eric Sosman
    Jan 7, 2005
  3. Lycan. Mao..
    Replies:
    4
    Views:
    3,343
    Lycan. Mao..
    Mar 4, 2007
  4. Reckoner
    Replies:
    11
    Views:
    742
    Steven D'Aprano
    Jan 19, 2009
  5. AikidoGuy
    Replies:
    11
    Views:
    596
    Seebs
    Nov 21, 2011
Loading...

Share This Page