[PyQt] Strange shadowing of hex() function by PyQt4.QtCore

Mark Summerfield mark at qtrac.eu
Fri Aug 8 08:02:55 BST 2008


On 2008-08-08, Boris Barbour wrote:
> Hi,
>
> Importing PyQt4.QtCore seems to alter or shadow the builtin hex()
> function. I'm afraid I haven't tracked things down further - I just
> learnt the hard way to "import" instead of "from import *". However,
> I'm not sure the clash is intended, so I'm reporting it.
>
> Best regards,
>
> Boris
[snip]

It is unfortunate that doing * imports on PyQt4 brings in some objects
which don't begin with q or Q. Here's a solution for hex shown as an
IDLE session:

    >>> hex(123)
    '0x7b'
    >>> from PyQt4.QtCore import *
    >>> hex(123)

    Traceback (most recent call last):
    File "<pyshell#3>", line 1, in <module>
	hex(123)
    TypeError: argument 1 of hex() has an invalid type
    >>> __builtins__.hex(123)
    '0x7b'
    >>> # restore built-in hex
    >>> hex = __builtins__.hex
    >>> hex(123) 
    '0x7b'

In Python 3 you'll be able to do "import builtins" and use
builtins.hex(). But I'm hoping that in PyQt4 for Python 3, the * imports
will only import objects that begin with q or Q, forcing the handful of
objects that don't meet this criterion to either be imported explicitly
or accessed fully qualified. [Any comment on this, Phil?]

-- 
Mark Summerfield, Qtrac Ltd, www.qtrac.eu
    C++, Python, Qt, PyQt - training and consultancy
        "Rapid GUI Programming with Python and Qt" - ISBN 0132354187



More information about the PyQt mailing list