⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 index.lxp@lxpwrap=c653_252ehtm.htm

📁 GUI Programming with Python
💻 HTM
📖 第 1 页 / 共 2 页
字号:
...     def __init__(self, value):...             self.item=value...     def printValue(self):...             print self.item...&#62;&#62;&#62; firstObject=FirstClass(value="BlackAdder goes forth")&#62;&#62;&#62; firstObject.printValue&#60;method FirstClass.printValue of FirstClass instance at 0x80db1f4&#62;&#62;&#62;&#62; firstObject.printValue()BlackAdder goes forth&#62;&#62;&#62;    </PRE><P>If you type neatly and without mistakes,      the contents of the Python interpreter window might look like      this. Let's look at what happens: we have defined a &#8216;class'      &#8212; that's a combination of information and complex actions      that work on the contained information. The class has a name:      <TTCLASS="VARNAME">FirstClass</TT>. (It is customary to capitalize      the first letter of each word in a classname).</P><P>A class in itself is only the      &#8216;template', so to speak, while an object is the      &#8216;document' &#8212; just as you can make documents out of      templates in a wordprocessor, you can make objects from      classes.</P><P>Furthermore, the class has two      &#8216;functions' &#8212; defined with the      <TTCLASS="FUNCTION">def</TT> statement.    </P><P>The first function,      <TTCLASS="FUNCTION">__init__</TT>, is called when you want to create      an object. The function has two &#8216;parameters' &#8212; that is,      two names associated with a value (which we call a &#8216;variable'      because the value can change, though the name remains the same).      The first parameter refers to the object &#8212; it's always      called <TTCLASS="VARNAME">self</TT> in Python (though it is called      <TTCLASS="VARNAME">this</TT> in Java or C++). The second parameter is      the value we want the object to manage for us.</P><P>You can use a dot &#8216;.' to associate      variables with each other. The line &#8216;self.item = value' means      that from now on the object we refer to with      <TTCLASS="VARNAME">self</TT> (but also, in another context, with      <TTCLASS="VARNAME">firstObject</TT>) knows that the name      <TTCLASS="VARNAME">item</TT> is associated with the value represented      by the parameter <TTCLASS="VARNAME">value</TT>.</P><P>Cleverly, Python doesn't forget this, so      when you create an object with the name      <TTCLASS="VARNAME">firstObject</TT> and the      <SPAN><ICLASS="EMPHASIS">string</I></SPAN> value (that is to say, some text, as      opposed to a number) &#8216;BlackAdder goes forth', you can later call      the <TTCLASS="FUNCTION">printValue()</TT> function, which will be      able to do something with that value.</P><P>In order to call&#8212;that is, ask Python to      execute&#8212; a function, you must add brackets after the      function name; the parameters always go between the brackets.      You don't have to put <TTCLASS="VARNAME">self</TT> between brackets, for      Python does this for you. If you don't add the brackets, you are      <SPAN><ICLASS="EMPHASIS">referring to</I></SPAN> the function, not asking      Python to execute it. Python then answers you with the revealing      sentence:</P><PRECLASS="SCREEN">&#62;&#62;&#62; firstObject.printValue&#60;method FirstClass.printValue of FirstClass instance at 0x80db1f4&#62;    </PRE><P>This tells you what kind of an object a      function is. <SPAN><ICLASS="EMPHASIS">Calling</I></SPAN> the function will      &#8216;print' the value of <TTCLASS="VARNAME">item</TT> in your      window:</P><PRECLASS="SCREEN">&#62;&#62;&#62; firstObject.printValue()BlackAdder goes forth&#62;&#62;&#62;    </PRE><P>As I said, the <TTCLASS="VARNAME">self</TT> is      supplied by Python, because you call the function from the      object. That is, by prefixing the variable that points to the      object to the function name, with a dot in between. This is the      same as typing the following code (that is, calling the function      with the object as its first parameter). As such, the following      two expressions are equivalent:</P><PRECLASS="SCREEN">&#62;&#62;&#62;firstObject.printValue()BlackAdder goes forth&#62;&#62;&#62;FirstClass.printValue(firstObject)BlackAdder goes forth    </PRE><P>Of course, typing in all these      instructions correctly every time you want the computer to print      &#8216;BlackAdder goes forth' is quite a chore. To get around this,      you can write a small text document (this is not the same as a      Word document!) using BlackAdder's text editor, and then ask      Python to execute it.</P><P>To sum up: composition of complex wholes      from smaller parts using a debased variant of English, and      calling things names, is what programming is all about. The rest      is made up of rules &#8212;rules intended to make it easier for      computer the compute to determine what it should do, and more      difficult for you to explain yourself to the machine.</P><DIVCLASS="WARNING"><P></P><TABLECLASS="WARNING"BORDER="1"WIDTH="100%"><TR><TDALIGN="CENTER"><B>Warning</B></TD></TR><TR><TDALIGN="LEFT"><P>Please be warned that if you execute        your programs (or scripts) from BlackAdder, all the output of        &#8216;print' will disappear into the void. The output will        only be shown if you start your scripts using the debugger,        and have the Python Interpreter window open. If you merely        type in stuff in the Interpreter window you will see all        output.</P></TD></TR></TABLE></DIV><P>If this section went over your head with      the airspeed of an unladen African swallow, don't worry. There      is much more to programming &#8212; more than I can explain in a      third of a chapter. Please read the Python tutorial that is      included with Python and with BlackAdder. It is well-written and      a little less hasty. Another good source is the free Livewires      Python course, which you can find in PDF format at:      http://www.livewires.org.uk/python/. I heartily recommend it as      the best introduction to the general idea of programming I've      ever read.</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"><A accesskey="P" href="index.lxp@lxpwrap=x650_252ehtm.htm">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><A accesskey="H" href="index.lxp@lxpwrap=book1_252ehtm">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><A accesskey="N" href="index.lxp@lxpwrap=x719_252ehtm.htm">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Conclusion</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><A accesskey="U" href="index.lxp@lxpwrap=p266_252ehtm.htm">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">The Rules</TD></TR></TABLE></DIV></BODY></HTML>      </td>      </tr>      </table>      </td>    </tr>  </table>      

⌨️ 快捷键说明

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