[prev in list] [next in list] [prev in thread] [next in thread] 

List:       pykde
Subject:    Re: [PyQt] from PyQt5.Qt import *
From:       "Zhao Lee" <redstone-cold () 163 ! com>
Date:       2018-06-29 2:35:38
Message-ID: 7d6f0cf1.2b.164496563f0.Coremail.redstone-cold () 163 ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]

[Attachment #4 (text/plain)]

You are absolutely right on the `import *` relevant issue .
I also agree with the "Importing Objects" part of book Rapid GUI Programming with \
Python and Qt: The Definitive Guide to PyQt  \
https://books.google.com/books?id=9oRa4WJLlGkC&lpg=PP1&pg=PT33#v=onepage&q&f=false


I choose to use the `import *` style in PyQt for the sake of brevity, and use the \
plain import syntax for other modules .






ÔÚ2018Äê06Ô 28 19ʱ27·Ö, "Kyle Altendorf"<sda@fstab.net>дµÀ:

On 2018-06-28 01:25, Zhao Lee wrote:
> but now I could replace all of them with only " from PyQt5.Qt import *
> "

> any drawback along with the single line import ?

The doc you linked explained the memory cost of importing anything via
the PyQt5.Qt module.  There are additional reasons that from imports are
somewhat risky and * imports are generally fairly questionable.

When you `from x import y` you are making a new name (`y`) referencing
the object referenced by `x.y` at that point in time.  Generally, module
globals (such as `y` in this example) should not be reassigned but it
can and does happen.  Then when you use `y` you will still get the old
object, not the new.

https://repl.it/@altendky/why-not-to-from-import

`from x import *` has the same issue, but additionally people reading
the code don't know where names came from.  With a single `import *`
it's annoying, with two...  we end up having to add diagnostics to the
code and run it ourselves to figure out where the otherwise unknown
variables came from.  Dumping things into the module scope with this
hazards some confusing behavior with some packages like numpy that have
lots of things with fairly 'normal' names in them.  You end up with
collisions and some names being unexpectedly overwritten possibly
depending on the order of imports.

Admittedly, Qt suffers from mostly the opposite namespacing issue in
that you end up triple namespaced because each layer of
PyQt5.QtWidgets.QApplication has the indicative Q in it.  Still, these
problems are generally relevant to `from` and `*` imports.

Cheers,
-kyle


[Attachment #5 (text/html)]

<div style=""><div style=""><div>You are absolutely right on the `import *` relevant \
issue .</div><div>I also agree with the "Importing Objects" part of book Rapid GUI \
Programming with Python and Qt: The Definitive Guide to \
PyQt&nbsp;</div><div>https://books.google.com/books?id=9oRa4WJLlGkC&amp;lpg=PP1&amp;pg=PT33#v=onepage&amp;q&amp;f=false</div><div><br></div><div>I \
choose to use the `import *` style in PyQt for the sake of brevity, and use the plain \
import syntax for other modules .</div><div><br></div></div><div style="color: rgb(0, \
0, 0); font-family: arial; font-size: 14px;"><br></div><div style="color: rgb(0, 0, \
0); font-family: arial; font-size: 14px;"><br></div></div><!-- jy5ContentSuffix \
--><div>ÔÚ2018Äê06Ô 28 19ʱ27·Ö, "Kyle \
Altendorf"&lt;sda@fstab.net&gt;дµÀ:</div><blockquote id="isReplyContent" \
style="padding-left:1ex; margin: 0px 0px 0px 0.8ex; BORDER-LEFT: #ccc 1px \
solid"><br>On 2018-06-28 01:25, Zhao Lee wrote:<br>&gt; but now I could replace all \
of them with only " from PyQt5.Qt import * <br>&gt; "<br><br>&gt; any drawback along \
with the single line import ?<br><br>The doc you linked explained the memory cost of \
importing anything via <br>the PyQt5.Qt module. &nbsp;There are additional reasons \
that from imports are <br>somewhat risky and * imports are generally fairly \
questionable.<br><br>When you `from x import y` you are making a new name (`y`) \
referencing <br>the object referenced by `x.y` at that point in time. \
&nbsp;Generally, module <br>globals (such as `y` in this example) should not be \
reassigned but it <br>can and does happen. &nbsp;Then when you use `y` you will still \
get the old <br>object, not the \
new.<br><br>https://repl.it/@altendky/why-not-to-from-import<br><br>`from x import *` \
has the same issue, but additionally people reading <br>the code don't know where \
names came from. &nbsp;With a single `import *` <br>it's annoying, with two... \
&nbsp;we end up having to add diagnostics to the <br>code and run it ourselves to \
figure out where the otherwise unknown <br>variables came from. &nbsp;Dumping things \
into the module scope with this <br>hazards some confusing behavior with some \
packages like numpy that have <br>lots of things with fairly 'normal' names in them. \
&nbsp;You end up with <br>collisions and some names being unexpectedly overwritten \
possibly <br>depending on the order of imports.<br><br>Admittedly, Qt suffers from \
mostly the opposite namespacing issue in <br>that you end up triple namespaced \
because each layer of <br>PyQt5.QtWidgets.QApplication has the indicative Q in it. \
&nbsp;Still, these <br>problems are generally relevant to `from` and `*` \
imports.<br><br>Cheers,<br>-kyle<br></blockquote><br><br><span \
title="neteasefooter"><p>&nbsp;</p></span>


[Attachment #6 (text/plain)]

_______________________________________________
PyQt mailing list    PyQt@riverbankcomputing.com
https://www.riverbankcomputing.com/mailman/listinfo/pyqt

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic