c2341.htm

来自「GUI Programming with Python」· HTM 代码 · 共 317 行

HTM
317
字号
<HTML><HEAD><TITLE>Python Objects and Qt Objects</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.72"><LINKREL="HOME"TITLE="GUI Programming with Python: QT Edition"HREF="book1.htm"><LINKREL="UP"TITLE="PyQt fundamentals"HREF="p1032.htm"><LINKREL="PREVIOUS"TITLE="Unicode strings"HREF="x2183.htm"><LINKREL="NEXT"TITLE="Circular references"HREF="x2377.htm"></HEAD><BODYCLASS="CHAPTER"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#0000FF"VLINK="#840084"ALINK="#0000FF"><DIVCLASS="NAVHEADER"><TABLESUMMARY="Header navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">GUI Programming with Python: QT Edition</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="x2183.htm"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom"></TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="x2377.htm"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="CHAPTER"><H1><ANAME="CH3">Chapter 9. Python Objects and Qt Objects</A></H1><DIVCLASS="TOC"><DL><DT><B>Table of Contents</B></DT><DT><AHREF="c2341.htm#AEN2346">Pointers and references</A></DT><DT><AHREF="x2377.htm">Circular references</A></DT><DT><AHREF="x2393.htm">Qt objects, Python objects and shadow objects</A></DT><DT><AHREF="x2420.htm">References and ownership</A></DT><DT><AHREF="x2540.htm">Other C++ objects</A></DT><DT><AHREF="x2549.htm">Connecting signals and slots</A></DT><DT><AHREF="x2568.htm">Object and class introspection</A></DT></DL></DIV><P>This chapter delves into the construction of    Python and C++ objects. This is a complex topic, and not really    required if you are only interested in getting started with your    project. However, when you feel that your objects are disappearing    from under your hands, or if you're leaking memory like a sieve,    then this is the place to turn to.    </P><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="AEN2346">Pointers and references</A></H1><P>In order to be able to determine the      relations between Python objects and C++ objects it is necessary      to first gain a good understanding of <SPAN><ICLASS="EMPHASIS">what</I></SPAN>      an object is, exactly, and what constitutes a reference to an      object.    </P><P>In C++, an object is simply a chunk of      memory that contains executable bytes and data bytes. The      executable bytes represent the functions, and the data bytes      represent the values of the object variables. Of course, this is      a simplified representation: the functions are shared by all      objects of the same class, and there is some serious (and      platform dependent) pointer logic needed to find them. But,      basically, a C++ object is simply a stretch of memory that has      to be allocated explicitly by the developer (using      <TTCLASS="FUNCTION">new()</TT>), and also deallocated explicitly by      the developer, with <TTCLASS="FUNCTION">delete()</TT>.    </P><P>The object can be accessed by other parts of      the application as long as its location in memory is known: the      variable that contains the location is a pointer. If a      programmer knows the size of an object, he can do fancy things      (such as loop through the memory by adding the size of the      object to the pointer) to get at the location of the next      object.    </P><P>However, once the pointer variable is lost,      there's no longer a certain way of getting at the location of      the object, and there's no way to delete the object&#8212;the      memory will remain occupied for as long as the application runs,      and there's <SPAN><ICLASS="EMPHASIS">no</I></SPAN> way it can be useful! This      is called a memory leak, and is undoubtedly a bad thing. </P><P>One of the strengths of Python is that the      programmer is freed of the responsibility of explicitly deleting      objects. Python manages all objects for you. It does this by      keeping track of <SPAN><ICLASS="EMPHASIS">references</I></SPAN> to every      object. A reference is a variable, or an entry in a list that      represents an object. For instance, run: </P><DIVCLASS="EXAMPLE"><ANAME="AEN2367"></A><P><B>Example 9-1. refs.py - showing object references</B></P><PRECLASS="PROGRAMLISTING">## refs.py#class theClass: passanObject=theClass()aList=[anObject]aDictionary={"key": anObject}print anObjectprint aListprint aDictionary      </PRE></DIV><P>This will result in one object with three references, as you      can see from the result of the <TTCLASS="FUNCTION">print</TT>      statements:</P><PRECLASS="SCREEN">&#60;__main__.theClass instance at 0x81d9cb4&#62;[&#60;__main__.theClass instance at 0x81d9cb4&#62;]{'key': &#60;__main__.theClass instance at 0x81d9cb4&#62;}</PRE><P>The object instance (0x81dcb4 is the object's id hash) will      only be deleted when the last reference is deleted. It is      possible for references to disappear by going out of scope. If      the references are created inside a function, then as soon as      the function is finished running, the references disappear.      References to variables can also be attached to both classes (a      class is an object in Python), and to objects. In the first      case, if the class disappears, then the references disappear. In      the second case, if the last reference to the object disappears,      all references that object &#8216;has' to other objects disappear,      too.</P></DIV></DIV><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLESUMMARY="Footer navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="x2183.htm"ACCESSKEY="P">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="book1.htm"ACCESSKEY="H">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="x2377.htm"ACCESSKEY="N">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Unicode strings</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="p1032.htm"ACCESSKEY="U">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Circular references</TD></TR></TABLE></DIV></BODY></HTML>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?