Statement evals as False in my IDE and True elsewhere

Discussion in 'Python' started by CM, Jan 30, 2014.

  1. CM

    CM Guest

    This is puzzling. (Using Python 2.5, WinXP, Boa Constructor 0.6.1 definitely running the code through Python 2.5)

    If I run these lines in my program, through my IDE (Boa Constructor),

    fake_data = ['n/a', 'n/a', 'n/a', 'n/a', '[omitted]', '12']
    fake_result = not all(i == '[omitted]' for i in fake_data)
    print 'This is fake result: ', fake_result

    I get this result:

    >>>

    This is fake result: False

    BUT, if I run those *exact same lines* (copied and pasted) in the Python 2.5 shell within Boa Constructor, or with IDLE with Python 2.5, I get:

    >>>

    This is fake result: True

    ....which is what it seems like it should evaluate to, right? What the heck is going on? How is this even possible? There is nothing that I know of in my code to cause this change, but perhaps there is. Otherwise I am at a total loss.

    Thanks,
    Che M
     
    CM, Jan 30, 2014
    #1
    1. Advertising

  2. CM

    Peter Otten Guest

    CM wrote:

    > This is puzzling. (Using Python 2.5, WinXP, Boa Constructor 0.6.1
    > definitely running the code through Python 2.5)
    >
    > If I run these lines in my program, through my IDE (Boa Constructor),
    >
    > fake_data = ['n/a', 'n/a', 'n/a', 'n/a', '[omitted]', '12']
    > fake_result = not all(i == '[omitted]' for i in fake_data)
    > print 'This is fake result: ', fake_result
    >
    > I get this result:
    >
    >>>>

    > This is fake result: False
    >
    > BUT, if I run those *exact same lines* (copied and pasted) in the Python
    > 2.5 shell within Boa Constructor, or with IDLE with Python 2.5, I get:
    >
    >>>>

    > This is fake result: True
    >
    > ...which is what it seems like it should evaluate to, right? What the
    > heck is going on? How is this even possible? There is nothing that I
    > know of in my code to cause this change, but perhaps there is. Otherwise
    > I am at a total loss.


    Hint:

    >>> def demo():

    .... fake_data = ['n/a', 'n/a', 'n/a', 'n/a', '[omitted]', '12']
    .... fake_result = not all(i == '[omitted]' for i in fake_data)
    .... print 'This is fake result: ', fake_result
    ....
    >>> demo()

    This is fake result: True
    >>> from numpy import all
    >>> demo()

    This is fake result: False
     
    Peter Otten, Jan 30, 2014
    #2
    1. Advertising

  3. On Fri, Jan 31, 2014 at 9:04 AM, CM <> wrote:
    > fake_data = ['n/a', 'n/a', 'n/a', 'n/a', '[omitted]', '12']
    > fake_result = not all(i == '[omitted]' for i in fake_data)
    > print 'This is fake result: ', fake_result


    Trying to get my head around this. You want to see if all the values
    in fake_data are '[omitted]' or not? That is to say, if there's
    anything that isn't '[omitted]'? Not sure that that's a normal thing
    to be asking, but that's what your code appears to do.

    What happens if you try this?

    fake_data = ['n/a', 'n/a', 'n/a', 'n/a', '[omitted]', '12']
    fake_result = set(fake_data)>{'[omitted]'}

    In theory, that should do the exact same thing as your code (returning
    True if there's anything in fake_data that is not '[omitted]').

    The other thing to try is peppering your code with print statements.
    Divide the work up into pieces - show the entire loop and what happens
    - print out everything you can imagine. See where the difference
    begins between inside and outside the IDE. Once you find that, you'll
    have a clue as to what's wrong.

    ChrisA
     
    Chris Angelico, Jan 30, 2014
    #3
  4. CM

    CM Guest

    On Thursday, January 30, 2014 5:14:57 PM UTC-5, Peter Otten wrote:

    > Hint:
    >
    > >>> def demo():

    > ... fake_data = ['n/a', 'n/a', 'n/a', 'n/a', '[omitted]', '12']
    > ... fake_result = not all(i == '[omitted]' for i in fake_data)
    > ... print 'This is fake result: ', fake_result
    >
    > >>> demo()

    > This is fake result: True
    >
    > >>> from numpy import all
    > >>> demo()

    > This is fake result: False


    That's brilliant, thanks! Now I'm just a bit unsure of what to do about it.. First, I don't actually have the line "from numpy import all" in that module's code, although I have imports of numpy; I think it is getting brought in through Matplotlib's pylab module, which I do import in that module.

    In any case, what's the most Pythonic way to deal with this? My first thought was to create the old all function and rename it so there would be no conflict:

    (both of what follows taken from answers here: http://stackoverflow.com/questions/18774388/re-import-aliased-shadowed-python-built-in-methods)

    builtin_all = __builtins__.all

    but I got the error:

    AttributeError: 'dict' object has no attribute 'all'

    So I wound up doing this:

    from __builtin__ import *

    which fixed the problem...but seems less than optimal, because what if I wanted to have numpy's all still in play?

    Again, thanks for restoring my faith in causality,
    Che M
     
    CM, Jan 30, 2014
    #4
  5. On Fri, Jan 31, 2014 at 9:48 AM, CM <> wrote:
    > builtin_all = __builtins__.all
    >
    > but I got the error:
    >
    > AttributeError: 'dict' object has no attribute 'all'


    Try using square brackets notation instead. Apparently your
    __builtins__ is a dictionary, not a module, though I don't know why
    (probably something to do with numpy, which I've never actually used).
    But try this:

    builtin_all = __builtins__["all"]

    It might work.

    ChrisA
     
    Chris Angelico, Jan 30, 2014
    #5
  6. CM

    CM Guest

    On Thursday, January 30, 2014 5:25:31 PM UTC-5, Chris Angelico wrote:
    > On Fri, Jan 31, 2014 at 9:04 AM, CM <> wrote:
    >
    > > fake_data = ['n/a', 'n/a', 'n/a', 'n/a', '[omitted]', '12']

    >
    > > fake_result = not all(i == '[omitted]' for i in fake_data)

    >
    > > print 'This is fake result: ', fake_result

    >
    >
    >
    > Trying to get my head around this. You want to see if all the values
    > in fake_data are '[omitted]' or not? That is to say, if there's
    > anything that isn't '[omitted]'? Not sure that that's a normal thing
    > to be asking, but that's what your code appears to do.


    That's what I want, yes. It probably sure isn't a normal thing to be asking, and I wouldn't be surprised if I am approaching it the wrong way. Essentially, if ALL the items in that list are '[omitted]', I must not process the list, but if even one of them is something other than '[omitted]', I need to process it.

    If there is a more Pythonic / better way to approach that, I'd like to knowit.

    > In theory, that should do the exact same thing as your code (returning
    > True if there's anything in fake_data that is not '[omitted]').


    Yes, as you saw and as Peter showed, that the builtin all was shadowed by numpy's all. I wouldn't have thought of that, but it makes sense now. Thesesorts of shadowing problems are so rare for me that I never think about that possibility.
     
    CM, Jan 30, 2014
    #6
  7. CM

    CM Guest

    > Try using square brackets notation instead. Apparently your
    > __builtins__ is a dictionary, not a module, though I don't know why
    > (probably something to do with numpy, which I've never actually used).
    >
    > But try this:
    > builtin_all = __builtins__["all"]
    >
    > It might work.


    Yes, it does. Thanks!

    Che M
     
    CM, Jan 30, 2014
    #7
  8. CM

    Peter Otten Guest

    CM wrote:

    > On Thursday, January 30, 2014 5:14:57 PM UTC-5, Peter Otten wrote:
    >
    >> Hint:
    >>
    >> >>> def demo():

    >> ... fake_data = ['n/a', 'n/a', 'n/a', 'n/a', '[omitted]', '12']
    >> ... fake_result = not all(i == '[omitted]' for i in fake_data)
    >> ... print 'This is fake result: ', fake_result
    >>
    >> >>> demo()

    >> This is fake result: True
    >>
    >> >>> from numpy import all
    >> >>> demo()

    >> This is fake result: False

    >
    > That's brilliant, thanks! Now I'm just a bit unsure of what to do about
    > it. First, I don't actually have the line "from numpy import all" in that
    > module's code, although I have imports of numpy; I think it is getting
    > brought in through Matplotlib's pylab module, which I do import in that
    > module.
    >
    > In any case, what's the most Pythonic way to deal with this? My first
    > thought was to create the old all function and rename it so there would be
    > no conflict:
    >
    > (both of what follows taken from answers here:
    > http://stackoverflow.com/questions/18774388/re-import-aliased-shadowed-

    python-built-in-methods)
    >
    > builtin_all = __builtins__.all
    >
    > but I got the error:
    >
    > AttributeError: 'dict' object has no attribute 'all'
    >
    > So I wound up doing this:
    >
    > from __builtin__ import *
    >
    > which fixed the problem...but seems less than optimal, because what if I
    > wanted to have numpy's all still in play?


    import numpy # you can now use numpy's all as numpy.all(...)
    del all # remove numpy's all from your module's global namespace and
    # thus make the builtin visible again
     
    Peter Otten, Jan 30, 2014
    #8
  9. On Fri, Jan 31, 2014 at 10:00 AM, CM <> wrote:
    > Essentially, if ALL the items in that list are '[omitted]', I must not process the list, but if even one of them is something other than '[omitted]', I need to process it.


    Okay. The set example that I gave will work, then - as long as all
    items are hashable (if they're strings, they are). Up to you which
    one's more readable.

    In any case, that could do with a supporting comment. My first
    suspicion was that it ought to be written as:

    fake_result = '[omitted]' not in fake_data

    and that it was calculating the wrong thing. But it is doing what you
    think it is.

    ChrisA
     
    Chris Angelico, Jan 30, 2014
    #9
  10. CM

    Terry Reedy Guest

    On 1/30/2014 5:55 PM, Chris Angelico wrote:
    > On Fri, Jan 31, 2014 at 9:48 AM, CM <> wrote:
    >> builtin_all = __builtins__.all
    >>
    >> but I got the error:
    >>
    >> AttributeError: 'dict' object has no attribute 'all'

    >
    > Try using square brackets notation instead. Apparently your
    > __builtins__ is a dictionary, not a module, though I don't know why


    For technical reasons Guido once explained and I have fogotten, it
    depends on whether you are in main module or an imported module -- and
    maybe the Python version.

    --
    Terry Jan Reedy
     
    Terry Reedy, Jan 31, 2014
    #10
  11. On Fri, Jan 31, 2014 at 4:05 PM, Terry Reedy <> wrote:
    > On 1/30/2014 5:55 PM, Chris Angelico wrote:
    >>
    >> On Fri, Jan 31, 2014 at 9:48 AM, CM <> wrote:
    >>>
    >>> builtin_all = __builtins__.all
    >>>
    >>> but I got the error:
    >>>
    >>> AttributeError: 'dict' object has no attribute 'all'

    >>
    >>
    >> Try using square brackets notation instead. Apparently your
    >> __builtins__ is a dictionary, not a module, though I don't know why

    >
    >
    > For technical reasons Guido once explained and I have fogotten, it depends
    > on whether you are in main module or an imported module -- and maybe the
    > Python version.


    Ah, okay. Anyway, the error message makes it clear. I love clear error messages.

    ChrisA
     
    Chris Angelico, Jan 31, 2014
    #11
  12. CM

    Terry Reedy Guest

    On 1/30/2014 6:00 PM, CM wrote:
    > On Thursday, January 30, 2014 5:25:31 PM UTC-5, Chris Angelico wrote:
    >> On Fri, Jan 31, 2014 at 9:04 AM, CM <> wrote:
    >>
    >>> fake_data = ['n/a', 'n/a', 'n/a', 'n/a', '[omitted]', '12']

    >>
    >>> fake_result = not all(i == '[omitted]' for i in fake_data)


    >> Trying to get my head around this. You want to see if all the values
    >> in fake_data are '[omitted]' or not? That is to say, if there's
    >> anything that isn't '[omitted]'? Not sure that that's a normal thing
    >> to be asking, but that's what your code appears to do.

    >
    > That's what I want, yes. It probably sure isn't a normal thing to be asking, and I wouldn't be surprised if I am approaching it the wrong way. Essentially, if ALL the items in that list are '[omitted]', I must not process the list, but if even one of them is something other than '[omitted]', I need to process it.
    >
    > If there is a more Pythonic / better way to approach that, I'd like to know it.


    not all(x) == any(not x), so...

    any(i != '[omitted]' for i in fake_data)

    While nothing you import should *ever* mask a builtin, this would also
    solve the all problem

    --
    Terry Jan Reedy
     
    Terry Reedy, Jan 31, 2014
    #12
    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. Siemel Naran

    Does true ^ true return false?

    Siemel Naran, Jun 17, 2004, in forum: C++
    Replies:
    19
    Views:
    668
    Chris Theis
    Jun 18, 2004
  2. André
    Replies:
    3
    Views:
    1,596
  3. bdb112
    Replies:
    45
    Views:
    1,347
    jazbees
    Apr 29, 2009
  4. Paul Burton

    Evals, quotes and backslashes problem

    Paul Burton, Sep 26, 2003, in forum: Perl Misc
    Replies:
    3
    Views:
    83
    Eric Amick
    Sep 27, 2003
  5. kj
    Replies:
    0
    Views:
    91
Loading...

Share This Page