Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

all() returns different values for the same expression

Tags:

python

I'm facing a very weird problem in my python script when I use the function all().

The console gives me false (which is obviously correct) for this line:

all(x == 2 for x in (8,2,2,2))

and in my script the same line returns true?!

What is going on here? Are there other all() function which could have overwritten it in my script? I'm importing the following modules:

import os
import sys
import string
import time
from time import gmtime, strftime
from optparse import OptionParser, OptionGroup
import cx_Oracle
from pylab import *
import ROOT
from array import array
import logging
from traceback import format_exc
like image 310
tamasgal Avatar asked Dec 03 '25 17:12

tamasgal


1 Answers

To access the builtin all() when it's being smashed, you can import builtins. E.g:

from builtins import all

(Below the line where pylab is imported).

Alternatively, if you need to access pylab.all(), you can do:

import builtins

...

builtins.all()

Or, better yet, do import pylab rather than from pylab import *.

You might want to file a bug report with pylab too, that's seriously bad behaviour. Although do note the import * from ... usage of imports is discouraged for this reason.

As DSM points out in the comments, this is presuming you are using 3.x, under 2.x, it is __builtin__.

like image 82
Gareth Latty Avatar answered Dec 10 '25 23:12

Gareth Latty



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!