📄 index.lxp@lxpwrap=x719_252ehtm.htm
字号:
<table border="0" cellspacing="0" cellpadding="3" width="100%"><tr><td> <div align="center" id="bldcontent"> <a href="../default.htm"><img src="../images/opendocs.png" width="63" height="76" border="0"></a> <br> <div class="symbol">Your OpenSource Publisher™</div> </div> </td></tr></table> <div align="center" class="author"> <a href="../products.lxp">Products</a> | <a href="../wheretobuy.lxp">Where to buy</a> | <a href="../bookstore.lxp">Retailers</a> | <a href="../faq.lxp">FAQ</a> | <a href="../writeforus.lxp">Write for Us.</a> | <a href="#contact">Contact Us.</a> </div> <table border="0" cellspacing="3" cellpadding="0" width="100%"><tr><td width="100%"> <div class="content"> <table border="0" cellspacing="2" cellpadding="0" width="100%"><tr><td width="100%"> <div align="center"><H4 CLASS="AUTHOR"><A NAME="AEN5">Boudewijn Rempt</A><br><a href="../../https@secure.linuxports.com/opendocs/default.htm"><img src=odpyqt125.png></a><br>ISBN: 0-97003300-4-4<br><a href="../../https@secure.linuxports.com/opendocs/default.htm">Available from bookstores everywhere or you can order it here.</a><p>You can download the source files for the book <a href="pyqtsrc.tgz">(code / eps) here.</a><hr></div> <HTML><HEAD><TITLE>The Rules</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="Introduction to Python"HREF="c653.htm"><LINKREL="PREVIOUS"TITLE="Introduction to Python"HREF="c653.htm"><LINKREL="NEXT"TITLE="Constructions"HREF="x823.htm"></HEAD><BODYCLASS="SECT1"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"><A accesskey="P" href="index.lxp@lxpwrap=c653_252ehtm.htm">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Chapter 4. Introduction to Python</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><A accesskey="N" href="index.lxp@lxpwrap=x823_252ehtm.htm">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1">The Rules</A></H1><P>For a full treatment of the rules of Python, see the Python Language Reference, which is available online with BlackAdder and Python. This section will in a series of short statements enumerate what makes Python Python.</P><DIVCLASS="SECT2"><H2CLASS="SECT2">Objects and references</A></H2><P>Before Python 2.2, not all types were classes, but now they are.</P><P>Moore's law has made type declarations obsolete (with thanks to Paul Prescod).</P><P>An object has a type (which you can query with <TTCLASS="FUNCTION">type()</TT>). A reference does not have a type. You can use the same name to refer to two objects in succession, but the first reference disappears as soon as you've made the second.</P><P>Objects disappear once the last reference has gone (except if the reference is an explicit weak reference). You can destroy a reference with <TTCLASS="FUNCTION">del</TT> — from that moment on, the name doesn't exist anymore. If you set the reference to <TTCLASS="VARNAME">None</TT>, the link to the object disappears, but the reference remains in existence.</P><PRECLASS="SCREEN">>>> a="aaa">>> print aaaa>>> del a>>> print aTraceback (most recent call last): File "<stdin>", line 1, in ?NameError: name 'a' is not defined>>> a="aaa">>> print aaaa>>> a=None>>> print aNone>>> </PRE><P>Functions and classes are both also objects.</P><P>Every object has one identity, which you can retrieve with <TTCLASS="FUNCTION">id()</TT>:</P><PRECLASS="SCREEN">>>> a=A()>>> id(a)135121324 </PRE><P>Some types are callable (i.e., put on a line with an argument list between ()) and can return a value. Callable types include classes, methods in clasess, functions and objects that implement the special method <TTCLASS="FUNCTION">__call__</TT>. </P></DIV><DIVCLASS="SECT2"><H2CLASS="SECT2">Formatting</A></H2><P>A block is first marked by a colon at the end of the previous line, and is indented. The block ends at the next dedent. (You should indent with four spaces, and not use tabs.)</P><P>Whatever is typed between brackets is considered to be on one line. Dictionaries are delimited with curlies {}, lists are delimited with brackets [] and tuples (and lists of arguments to functions) are delimited with ().</P><P>A classname should start with a capital letter; variable and function names should begin with a lowercase letter.</P><P>Only alphabetic characters (a-z, A-Z), digits (0-9) and the underscore (_) are valid in variable names, but a variable name should not start with a digit.</P><P>Names that start with one underscore (_) are a bit private (not imported with <TTCLASS="USERINPUT"><B>from module import *</B></TT>); names that start with two underscores (__) are very private in scope (not visible with <TTCLASS="USERINPUT"><B>dir(object)</B></TT>); names that start and end with two underscores are system-defined.</P><PRECLASS="SCREEN">Python 2.1.1 (#1, Aug 11 2001, 20:14:53)[GCC 2.95.2 19991024 (release)] on linux2Type "copyright", "credits" or "license" for more information.>>> class A:... def __test():... pass...>>> dir(A)['_A__test', '__doc__', '__module__']>>> a=A()>>> dir (a)[] </PRE></DIV><DIVCLASS="SECT2"><H2CLASS="SECT2">Keywords</A></H2><P>The following keywords are reserved: </P><PRECLASS="SCREEN">and del for is raiseassert elif from lambda returnbreak else global not tryclass except if or whilecontinue exec import pass yielddef finally in print </PRE></DIV><DIVCLASS="SECT2"><H2CLASS="SECT2">Literals</A></H2><P>Strings can be enclosed in single (' or ") or triple (''' or """") quotes. Triple-quoted strings can span lines, the linebreaks are part of the string. If you prefix the string literal with u, it becomes a Unicode string.</P><P>Numbers can be integers, long integers, floating point, and imaginary. If you divide integers or long integers, you will not get a float, but the integer before the decimal symbol (unless you <TTCLASS="LITERAL">import division from future</TT> in Python 2.2).</P><P>Python has the following operators:</P><PRECLASS="SCREEN">+ — * ** / %<< >> & | ^ ~< > <= >= == != <> </PRE><P>The comparison operators <TTCLASS="FUNCTION"><></TT> and <TTCLASS="FUNCTION">!=</TT> are alternate spellings of the same operator. <TTCLASS="FUNCTION">!=</TT> is the preferred spelling; <TTCLASS="FUNCTION"><></TT> is obsolescent.</P></DIV><DIVCLASS="SECT2"><H2CLASS="SECT2">Methods and functions</A></H2><P>Functions are callable objects that return a value (if a function doesn't explicitly return a value, it retuns <TTCLASS="VARNAME">None</TT>). Methods are the same, but part of a class. A method's argument list always has <TTCLASS="VARNAME">self</TT> (which refers to the class instance) as its first argument.</P><P>A function can be called with positional arguments, or named arguments. When mixed, positional arguments come first. </P><P>A variable number of positional arguments is indicated by <TTCLASS="VARNAME">*args</TT>, and a variable number of named arguments is indicated by <TTCLASS="VARNAME">**args</TT>. You can access <TTCLASS="VARNAME">*args</TT> as a tuple in your function, and <TTCLASS="VARNAME">**args</TT> as a dictionary in your function.</P><PRECLASS="SCREEN">>>> def f(a):... print a...>>> def ff(a, b):... print a, b...>>> def fff(*args):... print args...>>> def ffff(**args):... print args...>>> f(1)1>>> ff(1, b=2)1 2>>> fff(1,2,3)(1, 2, 3)>>> ffff(a=1,b=2,c=3){'b': 2, 'c': 3, 'a': 1}>>></PRE></DIV><DIVCLASS="SECT2"><H2CLASS="SECT2">High level datatypes</A></H2><P>Python has three very high level datatypes: tuples, lists and dictionaries.</P><P>A tuple is any combination of unique objects. You can't change the composition of items in a tuple (i.e. substitute another object), although the objects themselves can be changed.</P><PRECLASS="SCREEN">>>> t=("a","b","c")>>> t('a', 'b', 'c')>>> t[2]="d"Traceback (most recent call last): File "<stdin>", line 1, in ?TypeError: object doesn't support item assignment>>> </PRE><P>A list is a list of objects. You can change which objects are in a list, adding and deleting items to your heart's delight.</P><PRECLASS="SCREEN">>>> l=["a", "b", "c"]>>> l[2]="d">>> l['a', 'b', 'd']>>> </PRE><P>A dictiony is a keyed list. Keys, which must be unchangeable (i.e. not lists) point to values. One key, one value. There can be no duplicate keys in a dictionary.</P><PRECLASS="SCREEN">>>> d={"a": "aaa", "b": "bbb", "c": "ccc"}>>> d{'b': 'bbb', 'c': 'ccc', 'a': 'aaa'}>>> d[2]Traceback (most recent call last): File "<stdin>", line 1, in ?KeyError: 2>>> d["b"]'bbb'>>> d["b"]="ddd">>> d{'b': 'ddd', 'c': 'ccc', 'a': 'aaa'}>>> </PRE></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=c653_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=x823_252ehtm.htm">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Introduction to Python</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><A accesskey="U" href="index.lxp@lxpwrap=c653_252ehtm.htm">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Constructions</TD></TR></TABLE></DIV></BODY></HTML> </td> </tr> </table> </td> </tr> </table>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -