Re: bool evaluations of generators vs lists

Discussion in 'Python' started by Josh Dukes, Feb 10, 2009.

  1. Josh Dukes

    Josh Dukes Guest

    > The first example is a list. A list of length 0 evaluates to False.
    >
    > The second example returns a generator object. A generator object
    > apparently evaluates to true. Your example is not iterating of their
    > values of the generator, but evaluating bool(generator_object) itself.
    > My feeling is that bool(generator_object) is ambiguous so shouldn't be
    > used to begin with.


    I was actually aware of that (thank you, though, for trying to help).
    What I was not clear on was if the boolean evaluation is a method of an
    object that can be modified via operatior overloading (in the same way
    + is actually .__add__()) or not. Clearly __nonzero__ is the operator I
    was curious about. Thanks for that info.

    > bool != has_values. Check python.org for how Python determines the
    > "truthiness" of an object. Generally speaking the following evaluate
    > to False:
    >
    > * None
    > * False
    > * zero of any numeric type, for example, 0, 0L, 0.0, 0j.
    > * any empty sequence, for example, '', (), [].
    > * any empty mapping, for example, {}.
    > * instances of user-defined classes, if the class defines a
    > __nonzero__() or __len__() method, when that method returns
    > the integer zero or bool value False.
    >
    > All other values are considered true -- so objects of many types are
    > always true.


    The thing I don't understand is why a generator that has no iterable
    values is different from an empty list. Why shouldn't bool ==
    has_value?? Technically a list, a tuple, and a string are also objects
    but if they lack values they're evaluated as False. It seems to me that
    a generator is an object that intends to replace lists where lazy
    evaluation would be more efficent. Here is one place where that's
    definitely true.
    The main reason I'm interested in this is that it improves performance
    immensely over boolean evaluation of large lists (as the attached code
    shows). It seems to me if I could use find a good use for it in my
    experimentation that someone else might also want to do the same thing
    in real-world code.

    Is there another list I should be asking these questions on?

    --

    Josh Dukes
    MicroVu IT Department
     
    Josh Dukes, Feb 10, 2009
    #1
    1. Advertising

  2. On Tue, 10 Feb 2009 12:50:02 -0800, Josh Dukes wrote:

    > The thing I don't understand is why a generator that has no iterable
    > values is different from an empty list.


    How do you know it has no iterable values until you call next() on it and
    get StopIteration?

    By the way, your "has_values" function is just a slower version of the
    built-in any().


    --
    Steven
     
    Steven D'Aprano, Feb 10, 2009
    #2
    1. Advertising

  3. Josh Dukes

    Chris Rebert Guest

    On Tue, Feb 10, 2009 at 1:57 PM, Steven D'Aprano
    <> wrote:
    > On Tue, 10 Feb 2009 12:50:02 -0800, Josh Dukes wrote:
    >
    >> The thing I don't understand is why a generator that has no iterable
    >> values is different from an empty list.

    >
    > How do you know it has no iterable values until you call next() on it and
    > get StopIteration?
    >
    > By the way, your "has_values" function is just a slower version of the
    > built-in any().


    <nitpick>
    Not quite: if the generator produces one or more elements but those
    elements happen to be boolean false according to Python, then any()
    will be false but has_values() will be true. The functions serve
    different purposes (produces at least 1 value vs. has at least one
    true value).

    Cheers,
    Chris

    --
    Follow the path of the Iguana...
    http://rebertia.com
     
    Chris Rebert, Feb 10, 2009
    #3
  4. Josh Dukes

    Josh Dukes Guest

    ahhh any! ok, yeah, I guess that's what I was looking for. Thanks.


    On 10 Feb 2009 21:57:56 GMT
    Steven D'Aprano <> wrote:

    > On Tue, 10 Feb 2009 12:50:02 -0800, Josh Dukes wrote:
    >
    > > The thing I don't understand is why a generator that has no iterable
    > > values is different from an empty list.

    >
    > How do you know it has no iterable values until you call next() on it
    > and get StopIteration?
    >
    > By the way, your "has_values" function is just a slower version of
    > the built-in any().
    >
    >



    --

    Josh Dukes
    MicroVu IT Department
     
    Josh Dukes, Feb 10, 2009
    #4
    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. JS#
    Replies:
    5
    Views:
    3,570
    Roedy Green
    Nov 21, 2005
  2. Josh Dukes
    Replies:
    0
    Views:
    310
    Josh Dukes
    Feb 10, 2009
  3. nil in evaluations

    , Apr 18, 2007, in forum: Ruby
    Replies:
    4
    Views:
    171
    Robert Dober
    Apr 18, 2007
  4. Tay Ray Chuan

    Help:Why does eval add a 1 behind evaluations?

    Tay Ray Chuan, Aug 28, 2003, in forum: Perl Misc
    Replies:
    3
    Views:
    115
    Tad McClellan
    Sep 3, 2003
  5. Mike Ballard

    problem using evaluations in vars

    Mike Ballard, Nov 21, 2005, in forum: Perl Misc
    Replies:
    2
    Views:
    77
    Anno Siegel
    Nov 21, 2005
Loading...

Share This Page