[PyQt] Not understanding instanceof() in this situation

J Barchan jnbarchan at gmail.com
Wed Aug 15 11:27:02 BST 2018


On 17 July 2018 at 17:38, Kyle Altendorf <sda at fstab.net> wrote:

> I _think_ I see what's going on.  You have a file named main.py (you
> probably meant `from main import Main`?) which you run directly such as
> `venv/bin/python main.py`.  When you do this it isn't `main` but rather
> `__main__` because you are running it directly and that's what Python does
> in that case.  When you import the `main.py` file you get another copy
> imported under the name `main`.  Each is separate including unique objects
> being created for the classes defined in `main.py`.
>
> I almost always use entry_points in my setup.py now instead of running
> files directly.  I also make sure to have an installable project any time I
> need to import my own code.  Not-installed imports are fiddly...  oh, and
> work in a virtualenv (basically required on linux), but i digress.
>
> https://github.com/altendky/basicpyqt5example/blob/
> 7bf5a5862d131f767df03632dc34e2b33c461a15/setup.py#L24
>
> Cheers,
>
> -kyle
>
>
> On 2018-07-17 08:45, J Barchan wrote:
>
> Sorry if this turns out to be a general question about Python's
> instanceof() rather than specifically PyQt.....
>
> For debugging leaks (in a simple fashion) I am walking
> QtWidgets.QApplication.allWidgets() looking at those whose parent() is
> None.
>
> I come across one I wish to ignore.  PyCharm debugger shows its class as
> __main__.Main.  My code has:
>
> class Main(QtWidgets.QMainWindow):
>     ...
>
> if __name__ == '__main__':
>     mainWindow = Main()
> So in my checker I try:
>
> from main.py import Main
> if isinstance(widget, Main): ...
>
> But this returns False on the widget.  I discovered I can just check
> instead for isinstance(widget, QtWidgets.QMainWindow), so I'm OK and
> that's better anyway.
>
> But I don't understand why it is not an instance of my Main given that is
> derived from QtWidgets.QMainWindow?  Could someone patiently enlighten me?
>
> --
> Kindest,
> Jonathan
>
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> https://www.riverbankcomputing.com/mailman/listinfo/pyqt
>
>
@Kyle
Sorry for long delay --- this email went to my *Spam* folder and I never
noticed it till now!

Your analysis is correct.

When you do this it isn't `main` but rather `__main__` because you are
> running it directly and that's what Python does in that case.  When you
> import the `main.py` file you get another copy imported under the name
> `main`.  Each is separate including unique objects being created for the
> classes defined in `main.py`.
>
I did not know that.  This would affect only my "main" (run from
command-line) module when importing, and it looks pretty disasterous.  I
shan't be importing that one again!  Thanks :)


-- 
Kindest,
Jonathan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20180815/daccd176/attachment.html>


More information about the PyQt mailing list