[prev in list] [next in list] [prev in thread] [next in thread]
List: pykde
Subject: Re: [PyQt] Not understanding instanceof() in this situation
From: Kyle Altendorf <sda () fstab ! net>
Date: 2018-07-17 16:38:36
Message-ID: cbd248dcb496d8d1501ed9137e2d536d () fstab ! net
[Download RAW message or body]
[Attachment #2 (multipart/alternative)]
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@riverbankcomputing.com
> https://www.riverbankcomputing.com/mailman/listinfo/pyqt
[Attachment #5 (unknown)]
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head><body \
style='font-size: 10pt'> <p>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`.</p> <p>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.</p> <p><a href="https://github.com/altendky/basicpyqt5example/blob/7bf5a5862d131f767df0 \
3632dc34e2b33c461a15/setup.py#L24">https://github.com/altendky/basicpyqt5example/blob/7bf5a5862d131f767df03632dc34e2b33c461a15/setup.py#L24</a></p>
<p>Cheers,</p>
<p>-kyle</p>
<p><br /></p>
<p>On 2018-07-17 08:45, J Barchan wrote:</p>
<blockquote type="cite" style="padding: 0 0.4em; border-left: #1010ff 2px solid; margin: \
0"><!-- html ignored --><!-- head ignored --><!-- meta ignored --> <div dir="ltr">
<div class="gmail_default" style="font-family: tahoma,sans-serif;">Sorry if this turns out to \
be a general question about Python's <span style="font-family: \
monospace,monospace;">instanceof()</span> rather than specifically PyQt.....</div> <div \
class="gmail_default" style="font-family: tahoma,sans-serif;"> </div> <div \
class="gmail_default" style="font-family: tahoma,sans-serif;">For debugging leaks (in a simple \
fashion) I am walking <span style="font-family: \
monospace,monospace;">QtWidgets.QApplication.allWidgets()</span> looking at those whose <span \
style="font-family: monospace,monospace;">parent() is None</span>.</div> <div \
class="gmail_default" style="font-family: tahoma,sans-serif;"> </div> <div \
class="gmail_default" style="font-family: tahoma,sans-serif;">I come across one I wish to \
<tt><span style="font-family: monospace, monospace;">ignore</span></tt>. PyCharm debugger \
shows its class as <span style="font-family: monospace,monospace;">__main__.Main</span>. \
My code has:</div> <div class="gmail_default" style="font-family: \
tahoma,sans-serif;"> </div> <div class="gmail_default" style="margin-left: 40px;"><span \
style="font-family: monospace,monospace;">class Main(QtWidgets.QMainWindow):</span></div> <div \
class="gmail_default" style="margin-left: 40px;"><span style="font-family: \
monospace,monospace;"> ...</span></div> <div class="gmail_default" \
style="margin-left: 40px;"><span style="font-family: monospace,monospace;"> </span></div> \
<div class="gmail_default" style="font-family: tahoma,sans-serif;"> <div style="margin-left: \
40px;"><span style="font-family: monospace,monospace;">if __name__ == '__main__':</span><br \
/><span style="font-family: monospace,monospace;"> mainWindow = \
Main()</span></div> </div>
<div class="gmail_default" style="font-family: tahoma,sans-serif;">So in my checker I \
try:</div> <div class="gmail_default" style="font-family: tahoma,sans-serif;"> </div>
<div class="gmail_default" style="margin-left: 40px;"><span style="font-family: \
monospace,monospace;">from main.py import Main</span></div> <div class="gmail_default" \
style="margin-left: 40px;"><span style="font-family: monospace,monospace;">if \
isinstance(widget, Main): ...</span></div> <div class="gmail_default" style="font-family: \
tahoma,sans-serif;"> </div> <div class="gmail_default" style="font-family: \
tahoma,sans-serif;">But this returns <span style="font-family: \
monospace,monospace;">False</span> on the widget. I discovered I can just check instead \
for <span style="font-family: monospace,monospace;">isinstance(widget, \
QtWidgets.QMainWindow)</span>, so I'm OK and that's better anyway.</div> <div \
class="gmail_default" style="font-family: tahoma,sans-serif;"> </div> <div \
class="gmail_default" style="font-family: tahoma,sans-serif;">But I don't understand why it is \
not an instance of my <span style="font-family: monospace,monospace;">Main</span> given that is \
derived from <span style="font-family: \
monospace,monospace;">QtWidgets.QMainWindow</span>? Could someone patiently enlighten \
me?</div> <br />-- <br />
<div class="gmail_signature">
<div dir="ltr">
<div>
<div dir="ltr">
<div><span style="font-family: tahoma,sans-serif;">Kindest,</span></div>
<div><span style="font-family: tahoma,sans-serif;">Jonathan</span></div>
</div>
</div>
</div>
</div>
</div>
<br />
<div class="pre" style="margin: 0; padding: 0; font-family: \
monospace">_______________________________________________<br /> PyQt mailing list \
<a \
href="mailto:PyQt@riverbankcomputing.com">PyQt@riverbankcomputing.com</a><br /><a \
href="https://www.riverbankcomputing.com/mailman/listinfo/pyqt" target="_blank" rel="noopener \
noreferrer">https://www.riverbankcomputing.com/mailman/listinfo/pyqt</a></div> </blockquote>
</body></html>
[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