<pre id="vvttv"><mark id="vvttv"><progress id="vvttv"></progress></mark></pre>
    <pre id="vvttv"></pre>

      <p id="vvttv"></p>

          <p id="vvttv"></p>

                <p id="vvttv"></p>

                <pre id="vvttv"><cite id="vvttv"><progress id="vvttv"></progress></cite></pre>

                  <output id="vvttv"><dfn id="vvttv"><th id="vvttv"></th></dfn></output>

                    <p id="vvttv"></p>

                    Previous Page

                    Up One Level

                    Next Page

                    Python ???

                    Contents

                    ????? 8. ??????? ????? Python ??? ??? 10. ????????


                    ?????



                     
                    9.
                    ??

                    Python?????????????μ???????????????????????????????????C++??Python's Modula-3?????Python?е??????????????????佨???????????????????????????????????“???????”?????????????????????????????????????????л?????????У??????????????override???????е??κη??????????п??????????е????????????????????????????????г????

                    ??C++?????????????е?????????????????????????????public????????е?????????????????virtual???????Modula-3???????????????????????????????????shorthands?????????????????????????????????????????????????????????????????????????????????????γ????????????????????????? This provides semantics for importing and renaming. ?????????C++????Modula-3????????????????????????????ò?????????????????±????????????????????????塣

                     
                    9.1
                    ?й?????????

                    ??????????????????????????Smalltalk??C++?н????Щ??????????Modula-3?????????????????????C++?????Python?????????????????????????????

                    ?????????????????????????????????????壬??Python??“????”?????????????????Python?в??????е?????????????????????????Щ??????????????????????Щ??????????????????????????????????C++??Modula-3????????Smalltalk?????????????Python????????????????????????????????????????д?????“????”??

                    ????????????????????????????????У?????????????????????????????е???????????Python????????л??????????????Щ????????????????????????????飩?????????????????????????Python???????????????????????????????漰????????壨????????????????????????????????????????????????????????????????????Щ????????????????磬????????????????????????????????????????????????????????????????????????????????????仯??????Pascal???????????????????????????

                     
                    9.2 Python
                    ??????????????

                    ?????????????????????Щ?й?Python????????????????????????????????????????????????????????????????????????????????????????????е????????κθ??Python?????????????á?

                    ??????Щ???忪???

                    ??????????????????????????????????????????Python??????????????????????????????????????????????????????п??????????????????????Щ????????????????????????? abs() ????????????????????????????????е???????????????????е?????????????????????????????????????????????????????????????????????????????????????е?????????κ????????????????????????????????????“maximize”?????????????????????????????????????????????????

                    ???????????Python???κ????“.”????????????????????磬????z.real?е?real?????z????????????????????????????????????????????????? modname.funcname?У?modname ??????????? funcname ???????????????????????????????е????????????????????????????????????? 9.1

                    ????????????????д????????????£???????????????????????????“modname.the_answer = 42????д?????????????del???????????磺“del modname.the_answer??? modname?????????the_answer ?????

                    ???????????????????????????в??????????????????????????????????Python????????????????????????????????????????????????????鶨?屻??????????????????????????????????浽?????????????????????????????е???????????????????ж?????????????????????__main__???????????????????????????????????????????????????????????????У?????????__builtin__????

                    ?????????????????????????????????????????????????δ????????????????????????????????????????У?????????????????????????????????

                    ?????????Python???????????????????????“??????”????????ζ?????????????е???????????????????η???

                    ?????????????????壬?????????????????????????????????????????????????????????????????????????????????????????????棬???????????????????????в???????????????????????????????????????????????????????????????

                    ??????????????????????????е??????????????????????????????м?????????????????????????????????????????????????

                    ???????????????????????????????????????????????????????????????????????????????????????????????????????????е?????????????

                    ????????????????????????????????????е???????????????????????????????????ú???????????????????λ????????????????????????棬????????????????????????????????????——?????Python??????????????????п???????????“????”????????????????????????????????????????????????????????

                    Python????????????????丳????????????????????????????????????——??????????????????????????“del x ”?????????????????????????????x????????????????????????????????????????????????import?????????????????????????????????????????global??佫???????????????????

                     
                    9.3
                    ?????

                    ????????????μ??????????μ?????????????Щ?μ????塣

                     
                    9.3.1
                    ??????

                    ???????????????£?

                    class ClassName:
                        <statement-1>
                        .
                        .
                        .
                        <statement-N>

                    ??????????????壨def????????????в?????Ч?????????????????if???????????????????????????????

                    ?????????????????????????????壬???????????????????????????——???????????????????????е?????????????????????????????????б????????????????——??????????????????Щ??

                    ?????????????????????μ???????????????????????????——??????????????????????????μ???????????????????彫?o???????????????

                    ????????????????????????????????????????????????????崴???????????????????????????????????????????????????????????????????????????Ч????????????????????????????????????????????????????ClassName????

                     
                    9.3.2
                    ?????

                    ????????????????????????ú????????

                    ??????????ú?Python?????е???????????????????? obj.name?????????????????????????е???????????Ч???????????????????????????

                    class MyClass:
                        "A simple example class"
                        i = 12345
                        def f(self):
                            return 'hello world'

                    ??? MyClass.i ?? MyClass.f ????Ч?????????????????????????????????????????????????????????????? MyClass.i ???????????? __doc__ ????????Ч????????????????????????“A simple example class??

                    ??????????ú?????????????????????????????μ????????????????????ɡ????磨?????????????? ??

                    x = MyClass()

                    ?????????????μ????????????????????????x??

                    ??????????????“????”???????????????????????????????????????????г????????????????????????__init__() ??????????????????????

                        def __init__(self):
                            self.data = []

                    ?????? __init__() ????????????????????????????′?????????????? __init__() ?????????????????У?????????????????μ??????

                    x = MyClass()

                    ?????????????????? __init__() ?????????в?????????????????? __init__()???????????????????????磺

                    >>> class Complex:
                    ...     def __init__(self, realpart, imagpart):
                    ...         self.r = realpart
                    ...         self.i = imagpart
                    ... 
                    >>> x = Complex(3.0, -4.5)
                    >>> x.r, x.i
                    (3.0, -4.5)

                     
                    9.3.3
                    ???????

                    ?????????????????????????????????Ψ????????????????????á?????????Ч??????????

                    ????????????????????????Smalltalk?е?“???????”??C++?е?“??????”???????????????????????????????????????????????????ɡ????磬???x????洴????MyClass ???????????δ????????16?????????κζ????????

                    x.counter = 1
                    while x.counter < 10:
                        x.counter = x.counter * 2
                    print x.counter
                    del x.counter

                    ???????????????????????????????????????????????????????????????Python?У????????????????????У?????????????????з????????磬?????????append??insert??remove??sort???????????????????????????????????????????????????

                    ??????????Ч??????????????????????壬???????У???????壩???????????????????е??????????????????????У?x..f???????Ч?????????????MyClass.f?????????????x.i????????MyClass.i??????????????x.f??MyClass.f??????????????  ?????????????????????????

                     
                    9.3.4
                    ????????

                    ?????????????????

                    x.f()

                    ????????????У???????????‘hello world’?????????????????????÷?????x.f???????????????????洢?????????á????磺

                    xf = x.f
                    while True:
                        print xf()

                    ???????“Hello world”??

                    ???÷?????????????????????????? x.f() ??????????????????????????f????????????????????????????????????????????????????????????????Python????????????????????????????????……

                    ???????????????μ?????????????????????????????????????????????????????????????????????У?????x.f() ????MyClass.f(x)?????????n?????????б?????????????????????????????????????б??????????????б????????????????

                    ???????????????????????????????????????а????????÷???????????????????????????????????????????????????Ч??????????????????????????????????????????????????????????????????????б???÷?????????????????2????????????????????б????????μ?????б??????????????????μ?????б??

                     
                    9.4
                    ?Щ???

                    ????Щ????????????????……??

                    ????????????????????????????????????????????????????????п??????????????bug??????????????????????????????????????????????????????д??????????????Сд??????????????????????????????????????????????????

                    ????????????????????????????????????????????á????仰????????????????????????????Python?????????????????????????????ж????????????????????????????Python?????????Cд????????б??????????C????дPython??????????????????????????????????

                    ??????С??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????Ч??????????????????????????????鷳??

                    ???????????????????????????????????????????????????????????????????????????????????????????????????????л????????????????????????

                    ????????????????????????? self???????????????????Python?????self ????????κ??????塣?????????????????????????????????Python??????????????????в?????????Щ?????????????????????????????

                    ???????е??κκ???????????????ж?????????????????????????????????д???????У???????????????????????е?????????????磺

                    # Function defined outside the class
                    def f1(self, x, y):
                        return min(x, x+y)
                    
                    class C:
                        f = f1
                        def g(self):
                            return 'hello world'
                        h = g

                    ???? f, g ?? h ??????C????????????????????????????????C????????????h??????g?????????????????????????????????

                    ??? self ????????????????????????????????????

                    class Bag:
                        def __init__(self):
                            self.data = []
                        def add(self, x):
                            self.data.append(x)
                        def addtwice(self, x):
                            self.add(x)
                            self.add(x)

                    ??????????????????????????????????????????????????????????????????????顣???????????????????????????????????????к???????????????????????????????????к?????????????????????????????????????????????????????????????????е???????????????????????????????????????????????????????????????????????????????????

                     
                    9.5
                    ???

                    ????????????????????о??“??”??????????塣???????????????????

                    class DerivedClassName(BaseClassName):
                        <statement-1>
                        .
                        .
                        .
                        <statement-N>

                    ???? BaseClassName ??????е??????????????????????????????????????????????????????????????????????????????????????

                    class DerivedClassName(modname.BaseClassName):

                    ???????????й?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

                    ??????????????????????????DerivedClassName() ??????е???????????????μ???????????????e????1????????????????????????????????????????????????????????????????????t????????

                    ???????????????????????????????????????????е??????????????????????????????????????????????????????????????????????е???????????????C++??????????Python?е????з?????????????鷽??????

                    ???????е????????????????????????????????????е???????????????????????????????????????????????“BaseClassName.methodname(self, arguments)??????????????????á????????????л???????????????????????????????á???

                     
                    9.5.1
                    ????

                    Python??????????????????????е???????????????

                    class DerivedClassName(Base1, Base2, Base3):
                        <statement-1>
                        .
                        .
                        .
                        <statement-N>

                    ????Ψ????????????????????????????????????????????????????????? DerivedClassName ??????е?????????????????????????????? Base1 ???????????????????????????????????????? Base2??????????

                    ????Щ?????????????????????Base1???????????Base2??Base3??????????????????????????Base1??Base2??????????????????????????????????????Base1????Base1??????С?????????????????????????????????塣??

                    ?????????????????л????????????Σ????Python????????????????????????????????????????????????????е?????????????????????ж???????????????????????????????壬????????????????????????????????????????“???????”????????????????????????????

                     
                    9.6
                    ??б???

                    Python???????г????????????????κ?????__spam ????????????????????????????β???漴???????? _classname__spam?? ????????????classname ??????????????????????????????????λ??????????????????????????????????????????????????????????????????????????б??????????????????255??????????????????????????????????????????????????????????

                    ???????????????????????ж???“???”??????????????????????????????????????????????????????????????е??????????????????????????????????????????????????е????????п??????????????????????????????????????????????????????ж??????????????????С??????????????????????????????????????б???????

                    ??????????exec??eval()??evalfile()???????????????????????????????global????????????global?????t?????“????”??????????????????????????????getattr() ??setattr()??delattr()????????????__dict__?????

                     
                    9.7
                    ????

                    ?????????Pascal??“?????record??”??C??“????struct??”????????????????????????????????????????????????????????????????????

                    class Employee:
                        pass
                    
                    john = Employee() # Create an empty employee record
                    
                    # Fill the fields of the record
                    john.name = 'John Doe'
                    john.dept = 'computer lab'
                    john.salary = 1000

                    ????Python???????????????????????????????????????????????????????????????????磬???????????????????????и?????????????????????????????Read()??Readline()????????????????????????????????????????????????????????????

                    ??????????????????? m.im_self ?????????????????????? m.im_func ?????????????????????

                     
                    9.8
                    ???????

                    ??????????????????????????????????????????????????

                    ???????????μ???Ч????????????????????

                    raise Class, instance
                    
                    raise instance

                    ?????????У?instance ?????? Class ???????????????????????????????????????д??

                    raise instance.__class__, instance

                    ???????????????????????????г?????????????????????????????????????????????????????????????????????????г?????????????????????????磬???′?????????B??C??D??

                    class B:
                        pass
                    class C(B):
                        pass
                    class D(C):
                        pass
                    
                    for c in [B, C, D]:
                        try:
                            raise c()
                        except D:
                            print "D"
                        except C:
                            print "C"
                        except B:
                            print "B"

                    ???????????????????????????“execpt B”????????????????B??B??B???????????????????????

                    ????????????????????????????????????????????e?????????????ú???str()???????????????????????

                     
                    9.9
                    ??????

                    ??????????????????????????????? for ??????

                    for element in [1, 2, 3]:
                        print element
                    for element in (1, 2, 3):
                        print element
                    for key in {'one':1, 'two':2}:
                        print key
                    for char in "123":
                        print char
                    for line in open("myfile.txt"):
                        print line

                    ?????????????????????????????????????÷???Python??????????????????for ??????????????е??? iter() ?? ?ú???????????????? next() ??????????????????????????????????????к???????????next() ?????? StopIteration ???? for ???????????????????乤???????????

                    >>> s = 'abc'
                    >>> it = iter(s)
                    >>> it
                    <iterator object at 0x00A1DB50>
                    >>> it.next()
                    'a'
                    >>> it.next()
                    'b'
                    >>> it.next()
                    'c'
                    >>> it.next()
                    
                    Traceback (most recent call last):
                      File "<pyshell#6>", line 1, in -toplevel-
                        it.next()
                    StopIteration

                    ??????????Э????????????????????????????????????????????????? __iter__() ?????????????????? next() ?????????????????????????? next()????? __iter__() ????????self??

                    >>> class Reverse:
                        "Iterator for looping over a sequence backwards"
                        def __init__(self, data):
                            self.data = data
                            self.index = len(data)
                        def __iter__(self):
                            return self
                        def next(self):
                            if self.index == 0:
                                raise StopIteration
                            self.index = self.index - 1
                            return self.data[self.index]
                    
                    >>> for char in Reverse('spam'):
                            print char
                    
                    m
                    a
                    p
                    s

                     
                    9.10
                    ??????

                    ??????????????????????????????????д????????????????????????????????????yield ?????? next() ?????????????????????????λ????????????????????е?λ?ú????е????????????????????????????????????????????

                    >>> def reverse(data):
                            for index in range(len(data)-1, -1, -1):
                                    yield data[index]
                                    
                    >>> for char in reverse('golf'):
                            print char
                    
                    f
                    l
                    o
                    g

                    ??????????????????????????????????????·?????????????????????????? __iter__() ?? next() ????????????????????

                    ????????????????????ε????????????????????????????????????????????????д?????????????? self.index ?? self.data ?????????????????

                    ???????????????????????????????????????????????????? StopIteration????????????????Щ????????д?????????????????????????????????



                    Footnotes

                    ... ???????!9.1
                    ??????????????????????????????????? __dict__ ????????????????????????????????? __dict__ ???????????????????????????????Υ??????????????????????????????????????С?

                    Previous Page

                    Up One Level

                    Next Page

                    Python ???

                    Contents

                    ????? 8. ??????? ????? Python ??? ??? 10. ????????


                    Release 2.3, documentation updated on July 29, 2003.

                    See About this document... for information on suggesting changes.
                    ?????, @ssv

                      <pre id="vvttv"><mark id="vvttv"><progress id="vvttv"></progress></mark></pre>
                      <pre id="vvttv"></pre>

                        <p id="vvttv"></p>

                            <p id="vvttv"></p>

                                  <p id="vvttv"></p>

                                  <pre id="vvttv"><cite id="vvttv"><progress id="vvttv"></progress></cite></pre>

                                    <output id="vvttv"><dfn id="vvttv"><th id="vvttv"></th></dfn></output>

                                      <p id="vvttv"></p>

                                      这里只有精品视频