[prev in list] [next in list] [prev in thread] [next in thread]
List: pykde
Subject: Re: [PyQt] Not understanding instanceof() in this situation
From: J Barchan <jnbarchan () gmail ! com>
Date: 2018-08-15 10:27:02
Message-ID: CABz3M_9vcEsC+yUEXZVaA_hi6OoW4syboXG0OYwcgWSDxGmfow () mail ! gmail ! com
[Download RAW message or body]
[Attachment #2 (multipart/alternative)]
On 17 July 2018 at 17:38, Kyle Altendorf <sda@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@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
[Attachment #5 (text/html)]
<div dir="ltr"><div class="gmail_default" \
style="font-family:tahoma,sans-serif"><br></div><div class="gmail_extra"><br><div \
class="gmail_quote">On 17 July 2018 at 17:38, Kyle Altendorf <span dir="ltr"><<a \
href="mailto:sda@fstab.net" target="_blank">sda@fstab.net</a>></span> \
wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px \
#ccc solid;padding-left:1ex"><div 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/7bf5a5862d131f767df03632dc34e2b33c461a15/setup.py#L24" \
target="_blank">https://github.com/altendky/<wbr>basicpyqt5example/blob/<wbr>7bf5a5862d131f767df03632dc34e2<wbr>b33c461a15/setup.py#L24</a></p>
<p>Cheers,</p>
<p>-kyle</p><div><div class="h5">
<p><br></p>
<p>On 2018-07-17 08:45, J Barchan wrote:</p>
</div></div><blockquote type="cite" style="padding:0 0.4em;border-left:#1010ff 2px \
solid;margin:0"><div><div class="h5"> <div dir="ltr">
<div 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 style="font-family:tahoma,sans-serif"> </div>
<div style="font-family:tahoma,sans-serif">For debugging leaks (in a simple fashion) \
I am walking <span style="font-family:monospace,monospace">QtWidgets.QApplication.<wbr>allWidgets()</span> \
looking at those whose <span style="font-family:monospace,monospace">parent() is \
None</span>.</div> <div style="font-family:tahoma,sans-serif"> </div>
<div 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 style="font-family:tahoma,sans-serif"> </div>
<div style="margin-left:40px"><span style="font-family:monospace,monospace">class \
Main(QtWidgets.QMainWindow):</span></div> <div style="margin-left:40px"><span \
style="font-family:monospace,monospace"> ...</span></div> <div \
style="margin-left:40px"><span style="font-family:monospace,monospace"> \
</span></div> <div 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 style="font-family:tahoma,sans-serif">So in my checker I try:</div>
<div style="font-family:tahoma,sans-serif"> </div>
<div style="margin-left:40px"><span style="font-family:monospace,monospace">from \
main.py import Main</span></div> <div style="margin-left:40px"><span \
style="font-family:monospace,monospace">if isinstance(widget, Main): ...</span></div> \
<div style="font-family:tahoma,sans-serif"> </div> <div \
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 \
style="font-family:tahoma,sans-serif"> </div> <div \
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="m_3041088181850644584gmail_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></div><div class="m_3041088181850644584pre" \
style="margin:0;padding:0;font-family:monospace">______________________________<wbr>_________________<br> \
PyQt mailing list <a href="mailto:PyQt@riverbankcomputing.com" \
target="_blank">PyQt@riverbankcomputing.com</a><br><a \
href="https://www.riverbankcomputing.com/mailman/listinfo/pyqt" rel="noopener \
noreferrer" target="_blank">https://www.<wbr>riverbankcomputing.com/<wbr>mailman/listinfo/pyqt</a></div>
</blockquote>
</div>
</blockquote></div><br><div style="font-family:tahoma,sans-serif" \
class="gmail_default">@Kyle</div><div style="font-family:tahoma,sans-serif" \
class="gmail_default">Sorry for long delay --- this email went to my <b>Spam</b> \
folder and I never noticed it till now!</div><div \
style="font-family:tahoma,sans-serif" class="gmail_default"><br></div><div \
style="font-family:tahoma,sans-serif" class="gmail_default">Your analysis is \
correct.</div><div style="font-family:tahoma,sans-serif" \
class="gmail_default"><br></div><blockquote class="gmail_quote" style="margin:0px 0px \
0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div \
style="font-family:tahoma,sans-serif" class="gmail_default">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`.<br></div></blockquote><div style="font-family:tahoma,sans-serif" \
class="gmail_default"></div><div style="font-family:tahoma,sans-serif" \
class="gmail_default">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 :)<br></div><br \
clear="all"><br>-- <br><div class="gmail_signature" \
data-smartmail="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></div>
[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