Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #65052 > unrolled thread

Statement evals as False in my IDE and True elsewhere

Started byCM <cmpython@gmail.com>
First post2014-01-30 14:04 -0800
Last post2014-01-31 00:09 -0500
Articles 12 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  Statement evals as False in my IDE and True elsewhere CM <cmpython@gmail.com> - 2014-01-30 14:04 -0800
    Re: Statement evals as False in my IDE and True elsewhere Peter Otten <__peter__@web.de> - 2014-01-30 23:14 +0100
      Re: Statement evals as False in my IDE and True elsewhere CM <cmpython@gmail.com> - 2014-01-30 14:48 -0800
        Re: Statement evals as False in my IDE and True elsewhere Chris Angelico <rosuav@gmail.com> - 2014-01-31 09:55 +1100
          Re: Statement evals as False in my IDE and True elsewhere CM <cmpython@gmail.com> - 2014-01-30 15:02 -0800
        Re: Statement evals as False in my IDE and True elsewhere Peter Otten <__peter__@web.de> - 2014-01-31 00:08 +0100
        Re: Statement evals as False in my IDE and True elsewhere Terry Reedy <tjreedy@udel.edu> - 2014-01-31 00:05 -0500
        Re: Statement evals as False in my IDE and True elsewhere Chris Angelico <rosuav@gmail.com> - 2014-01-31 16:07 +1100
    Re: Statement evals as False in my IDE and True elsewhere Chris Angelico <rosuav@gmail.com> - 2014-01-31 09:25 +1100
      Re: Statement evals as False in my IDE and True elsewhere CM <cmpython@gmail.com> - 2014-01-30 15:00 -0800
        Re: Statement evals as False in my IDE and True elsewhere Chris Angelico <rosuav@gmail.com> - 2014-01-31 10:25 +1100
        Re: Statement evals as False in my IDE and True elsewhere Terry Reedy <tjreedy@udel.edu> - 2014-01-31 00:09 -0500

#65052 — Statement evals as False in my IDE and True elsewhere

FromCM <cmpython@gmail.com>
Date2014-01-30 14:04 -0800
SubjectStatement evals as False in my IDE and True elsewhere
Message-ID<a543e1a5-ef00-42b0-96df-5bf9ee8ac74c@googlegroups.com>
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

[toc] | [next] | [standalone]


#65054

FromPeter Otten <__peter__@web.de>
Date2014-01-30 23:14 +0100
Message-ID<mailman.6169.1391120094.18130.python-list@python.org>
In reply to#65052
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

[toc] | [prev] | [next] | [standalone]


#65058

FromCM <cmpython@gmail.com>
Date2014-01-30 14:48 -0800
Message-ID<13a1aca8-a246-4618-925f-05ebf6c2e950@googlegroups.com>
In reply to#65054
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

[toc] | [prev] | [next] | [standalone]


#65061

FromChris Angelico <rosuav@gmail.com>
Date2014-01-31 09:55 +1100
Message-ID<mailman.6175.1391122543.18130.python-list@python.org>
In reply to#65058
On Fri, Jan 31, 2014 at 9:48 AM, CM <cmpython@gmail.com> 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

[toc] | [prev] | [next] | [standalone]


#65064

FromCM <cmpython@gmail.com>
Date2014-01-30 15:02 -0800
Message-ID<f78ce3e8-353e-4f8f-b61f-6b406a1ea281@googlegroups.com>
In reply to#65061
> 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

[toc] | [prev] | [next] | [standalone]


#65066

FromPeter Otten <__peter__@web.de>
Date2014-01-31 00:08 +0100
Message-ID<mailman.6177.1391123287.18130.python-list@python.org>
In reply to#65058
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


[toc] | [prev] | [next] | [standalone]


#65089

FromTerry Reedy <tjreedy@udel.edu>
Date2014-01-31 00:05 -0500
Message-ID<mailman.6190.1391144738.18130.python-list@python.org>
In reply to#65058
On 1/30/2014 5:55 PM, Chris Angelico wrote:
> On Fri, Jan 31, 2014 at 9:48 AM, CM <cmpython@gmail.com> 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

[toc] | [prev] | [next] | [standalone]


#65090

FromChris Angelico <rosuav@gmail.com>
Date2014-01-31 16:07 +1100
Message-ID<mailman.6191.1391144879.18130.python-list@python.org>
In reply to#65058
On Fri, Jan 31, 2014 at 4:05 PM, Terry Reedy <tjreedy@udel.edu> wrote:
> On 1/30/2014 5:55 PM, Chris Angelico wrote:
>>
>> On Fri, Jan 31, 2014 at 9:48 AM, CM <cmpython@gmail.com> 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

[toc] | [prev] | [next] | [standalone]


#65056

FromChris Angelico <rosuav@gmail.com>
Date2014-01-31 09:25 +1100
Message-ID<mailman.6171.1391121221.18130.python-list@python.org>
In reply to#65052
On Fri, Jan 31, 2014 at 9:04 AM, CM <cmpython@gmail.com> 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

[toc] | [prev] | [next] | [standalone]


#65063

FromCM <cmpython@gmail.com>
Date2014-01-30 15:00 -0800
Message-ID<5673ebd0-89b6-485c-8ccb-f62bf15f513a@googlegroups.com>
In reply to#65056
On Thursday, January 30, 2014 5:25:31 PM UTC-5, Chris Angelico wrote:
> On Fri, Jan 31, 2014 at 9:04 AM, CM <cmpython@gmail.com> 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 know it.

> 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. These sorts of shadowing problems are so rare for me that I never think about that possibility.

[toc] | [prev] | [next] | [standalone]


#65067

FromChris Angelico <rosuav@gmail.com>
Date2014-01-31 10:25 +1100
Message-ID<mailman.6178.1391124349.18130.python-list@python.org>
In reply to#65063
On Fri, Jan 31, 2014 at 10:00 AM, CM <cmpython@gmail.com> 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

[toc] | [prev] | [next] | [standalone]


#65092

FromTerry Reedy <tjreedy@udel.edu>
Date2014-01-31 00:09 -0500
Message-ID<mailman.6193.1391145004.18130.python-list@python.org>
In reply to#65063
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 <cmpython@gmail.com> 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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web