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

📄 sect01.htm

📁 this is the most basic to learn python
💻 HTM
📖 第 1 页 / 共 2 页
字号:
seen. You can use single or double quotes to represent strings, which is very
nice because if you surround a string with double quotes, you can embed single
quotes and vice versa:
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A2_0018">Add Comment</A></FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>#: c01:strings.py
<font color=#0000ff>print</font> <font color=#004488>"That isn't a horse"</font>
<font color=#0000ff>print</font> 'You are <font color=#0000ff>not</font> a <font color=#004488>"Viking"</font>'
<font color=#0000ff>print</font> <font color=#004488>""</font>"You're just pounding two
coconut halves together.<font color=#004488>""</font>"
<font color=#0000ff>print</font> '''<font color=#004488>"Oh no!"</font> He exclaimed.
<font color=#004488>"It's the blemange!"</font>'''
<font color=#0000ff>print</font> r'c:\python\lib\utils'
#:~</PRE></FONT></BLOCKQUOTE><DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Note that Python was not named
after the snake, but rather the Monty Python comedy troupe, and so examples are
virtually required to include Python-esque references.
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A2_0019">Add Comment</A></FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The triple-quote syntax quotes
everything, including newlines. This makes it particularly useful for doing
things like generating web pages (Python is an especially good CGI language),
since you can just triple-quote the entire page that you want without any other
editing.
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A2_0020">Add Comment</A></FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The &#145;<B>r</B>&#146; right before a
string means &#147;raw,&#148; which takes the backslashes literally so you
don&#146;t have to put in an extra backslash in order to insert a literal
backslash.
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A2_0021">Add Comment</A></FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Substitution in strings is exceptionally
easy, since Python uses C&#146;s <B>printf(&#160;)</B> substitution syntax, but
for any string at all. You simply follow the string with a
&#145;<B>%</B>&#146; and the values to substitute:
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A2_0022">Add Comment</A></FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>#: c01:stringFormatting.py
val = 47
<font color=#0000ff>print</font> <font color=#004488>"The number is %d"</font> % val
val2 = 63.4
s = <font color=#004488>"val: %d, val2: %f"</font> % (val, val2)
<font color=#0000ff>print</font> s
#:~</PRE></FONT></BLOCKQUOTE><DIV ALIGN="LEFT"><P><FONT FACE="Georgia">As you can see in the second
case, if you have more than one argument you surround them in parentheses (this
forms a <I>tuple</I>, which is a list that cannot be modified &#150; you can
also use regular lists for multiple arguments, but tuples are typical).
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A2_0023">Add Comment</A></FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">All the formatting from
<B>printf(&#160;)</B> is available, including control over the number of decimal
places and alignment. Python also has very sophisticated regular expressions.
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A2_0024">Add Comment</A></FONT><A NAME="_Toc524504147"></A><A NAME="_Toc534420061"></A><BR></P></DIV>
<A NAME="Heading10"></A><FONT FACE = "Verdana, Tahoma, Arial, Helvetica, Sans"><H3 ALIGN="LEFT">
Classes</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Like everything else in Python, the
definition of a class uses a minimum of additional syntax. You use the
<B>class</B> keyword, and inside the body you use <B>def</B> to create methods.
Here&#146;s a simple class:
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A2_0025">Add Comment</A></FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>#: c01:SimpleClass.py
<font color=#0000ff>class</font> Simple:
  <font color=#0000ff>def</font> __init__(self, str):
    <font color=#0000ff>print</font> <font color=#004488>"Inside the Simple constructor"</font>
    self.s = str
  # Two methods:
  <font color=#0000ff>def</font> show(self):
    <font color=#0000ff>print</font> self.s
  <font color=#0000ff>def</font> showMsg(self, msg):
    <font color=#0000ff>print</font> msg + ':',
    self.show() # Calling another method

<font color=#0000ff>if</font> __name__ == <font color=#004488>"__main__"</font>:
  # Create an object:
  x = Simple(<font color=#004488>"constructor argument"</font>)
  x.show()
  x.showMsg(<font color=#004488>"A message"</font>)
#:~</PRE></FONT></BLOCKQUOTE><DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Both methods have
&#147;<B>self</B>&#148;<B> </B>as their first argument. C++ and Java both have
a hidden first argument in their class methods, which points to the object that
the method was called for and can be accessed using the keyword <B>this</B>.
Python methods also use a reference to the current object, but when you are
<I>defining</I> a method you must explicitly specify the reference as the first
argument. Traditionally, the reference is called <B>self</B> but you could use
any identifier you want (if you do not use <B>self</B> you will probably confuse
a lot of people, however). If you need to refer to fields in the object or other
methods in the object, you must use <B>self</B> in the expression. However, when
you call a method for an object as in <B>x.show(&#160;)</B>, you do not hand it
the reference to the object &#150; <I>that</I> is done for you.
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A2_0026">Add Comment</A></FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Here, the first method is special, as is
any identifier that begins and ends with double underscores. In this case, it
defines the constructor, which is automatically called when the object is
created, just like in C++ and Java. However, at the bottom of the example you
can see that the creation of an object looks just like a function call using the
class name. Python&#146;s spare syntax makes you realize that the <B>new</B>
keyword isn&#146;t really necessary in C++ or Java, either.
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A2_0027">Add Comment</A></FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">All the code at the bottom is set off by
an <B>if</B> clause, which checks to see if something called <B>__name__</B> is
equivalent to <B>__main__</B>. Again, the double underscores indicate special
names. The reason for the <B>if</B> is that any file can also be used as a
library module within another program (modules are described shortly). In that
case, you just want the classes defined, but you don&#146;t want the code at
the bottom of the file to be executed. This particular <B>if</B> statement is
only true when you are running this file directly; that is, if you say on the
command line:
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A2_0028">Add Comment</A></FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>Python SimpleClass.py</PRE></FONT></BLOCKQUOTE><DIV ALIGN="LEFT"><P><FONT FACE="Georgia">However,
if this file is imported as a module into another program, the <B>__main__</B>
code is not executed.
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A2_0029">Add Comment</A></FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Something that&#146;s a little
surprising at first is that you define fields inside methods, and not outside of
the methods like C++ or Java (if you create fields using the C++/Java style,
they implicitly become static fields). To create an object field, you just name
it &#150; using <B>self</B> &#150; inside of one of the methods (usually in
the constructor, but not always), and space is created when that method is run.
This seems a little strange coming from C++ or Java where you must decide ahead
of time how much space your object is going to occupy, but it turns out to be a
very flexible way to program.
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A2_0030">Add Comment</A></FONT><BR></P></DIV>
<A NAME="Heading11"></A><FONT FACE = "Verdana, Tahoma, Arial, Helvetica, Sans"><H4 ALIGN="LEFT">
Inheritance</H4></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Because Python is weakly typed, it
doesn&#146;t really care about interfaces &#150; all it cares about is
applying operations to objects (in fact, Java&#146;s <B>interface</B> keyword
would be wasted in Python). This means that inheritance in Python is different
from inheritance in C++ or Java, where you often inherit simply to establish a
common interface. In Python, the only reason you inherit is to inherit an
implementation &#150; to re-use the code in the base class. 
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A2_0031">Add Comment</A></FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">If you&#146;re going to inherit from a
class, you must tell Python to bring that class into your new file. Python
controls its name spaces as aggressively as Java does, and in a similar fashion
(albeit with Python&#146;s penchant for simplicity). Every time you create a
file, you implicitly create a module (which is like a package in Java) with the
same name as that file. Thus, no <B>package</B> keyword is needed in Python.
When you want to use a module, you just say <B>import</B> and give the name of
the module. Python searches the PYTHONPATH in the same way that Java searches
the CLASSPATH (but for some reason, Python doesn&#146;t have the same kinds of
pitfalls as Java does) and reads in the file. To refer to any of the functions
or classes within a module, you give the module name, a period, and the function
or class name. If you don&#146;t want the trouble of qualifying the name, you
can say </FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>from </B><I>module</I> <B>import</B>
<I>name(s)</I></FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Where &#147;name(s)&#148; can be a list
of names separated by commas.
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A2_0032">Add Comment</A></FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">You inherit a class (or classes &#150;
Python supports multiple inheritance) by listing the name(s) of the class inside
parentheses after the name of the inheriting class. Note that the <B>Simple</B>
class, which resides in the file (and thus, module) named <B>SimpleClass</B> is
brought into this new name space using an <B>import</B> statement:
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A2_0033">Add Comment</A></FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>#: c01:Simple2.py
<font color=#0000ff>from</font> SimpleClass <font color=#0000ff>import</font> Simple

<font color=#0000ff>class</font> Simple2(Simple):
  <font color=#0000ff>def</font> __init__(self, str):
    <font color=#0000ff>print</font> <font color=#004488>"Inside Simple2 constructor"</font>
    # You must explicitly call 
    # the base-<font color=#0000ff>class</font> constructor:
    Simple.__init__(self, str)
  <font color=#0000ff>def</font> display(self):
    self.showMsg(<font color=#004488>"Called from display()"</font>)
  # Overriding a base-<font color=#0000ff>class</font> method
  <font color=#0000ff>def</font> show(self):
    <font color=#0000ff>print</font> <font color=#004488>"Overridden show() method"</font>
    # Calling a base-<font color=#0000ff>class</font> method <font color=#0000ff>from</font> inside
    # the overridden method:
    Simple.show(self)

<font color=#0000ff>class</font> Different:
  <font color=#0000ff>def</font> show(self):
    <font color=#0000ff>print</font> <font color=#004488>"Not derived from Simple"</font>

<font color=#0000ff>if</font> __name__ == <font color=#004488>"__main__"</font>:
  x = Simple2(<font color=#004488>"Simple2 constructor argument"</font>)
  x.display()
  x.show()
  x.showMsg(<font color=#004488>"Inside main"</font>)
  <font color=#0000ff>def</font> f(obj): obj.show() # One-line definition
  f(x)
  f(Different())
#:~</PRE></FONT></BLOCKQUOTE><DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>Simple2</B> is inherited from
<B>Simple</B>, and in the constructor, the base-class constructor is called. In
<B>display(&#160;)</B>, <B>showMsg(&#160;)</B> can be called as a method of
<B>self</B>, but when calling the base-class version of the method you are
overriding, you must fully qualify the name and pass <B>self</B> in as the first
argument, as shown in the base-class constructor call. This can also be seen in
the overridden version of <B>show(&#160;)</B>.
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A2_0034">Add Comment</A></FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">In <B>__main__</B>, you will see (when
you run the program) that the base-class constructor is called. You can also see
that the <B>showMsg(&#160;)</B> method is available in the derived class, just
as you would expect with inheritance.
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A2_0035">Add Comment</A></FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The class <B>Different</B> also has a
method named <B>show(&#160;)</B>, but this class is not derived from
<B>Simple</B>. The <B>f(&#160;)</B> method defined in <B>__main__</B>
demonstrates weak typing: all it cares about is that <B>show(&#160;)</B> can be
applied to <B>obj</B>, and it doesn&#146;t have any other type requirements.
You can see that <B>f(&#160;)</B> can be applied equally to an object of a class
derived from <B>Simple</B> and one that isn&#146;t, without discrimination. If
you&#146;re a C++ programmer, you should see that the objective of the C++
<B>template</B> feature is exactly this: to provide weak typing in a
strongly-typed language. Thus, in Python you automatically get the equivalent of
templates &#150; without having to learn that particularly difficult syntax and
semantics.
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A2_0036">Add Comment</A></FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">[[ Suggest Further Topics for inclusion
in the introductory chapter ]]</FONT><FONT FACE="Georgia">
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A2_0037">Add Comment</A></FONT><BR></P></DIV>

<DIV ALIGN="CENTER">
    <FONT FACE="Verdana, Tahoma, Arial, Helvetica, Sans" size = "-1">
     [ <a href="Intro.htm">Previous Chapter</a> ] 
    
    [ <a href="javascript:window.location.href = 'Index.htm';">Table of Contents</a> ] 
  
        [ <a href="DocIdx.htm">Index</a> ]
        
     [ <a href="Sect02.htm">Next Chapter</a> ] 
    </FONT>
    <BR>
 Last Update:12/31/2001</P></DIV>

</BODY>

</HTML>

⌨️ 快捷键说明

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