Statement evals as False in my IDE and True elsewhere

CM cmpython at gmail.com
Thu Jan 30 18:00:34 EST 2014


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 at 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.



More information about the Python-list mailing list