Statement evals as False in my IDE and True elsewhere
Peter Otten
__peter__ at web.de
Thu Jan 30 18:08:08 EST 2014
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
More information about the Python-list
mailing list