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

Mark Summerfield mark at qtrac.eu
Fri Aug 8 12:15:21 BST 2008


On 2008-08-08, Phil Thompson wrote:
> On Fri, 8 Aug 2008 08:02:55 +0100, Mark Summerfield <mark at qtrac.eu> wrote:
> > 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?]
>
> I'm not going to treat specific names differently as it's not a PyQt
> problem.
>
> Don't use "import *" - ever, in any Python code.

Even for < 1000 line utilities or for interactive experimentation?

Anyway, I still think you should define __all__ to only include objects
that match ^[Qq]\w+$


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



More information about the PyQt mailing list