<span id="7ztzv"></span>
<sub id="7ztzv"></sub>

<span id="7ztzv"></span><form id="7ztzv"></form>

<span id="7ztzv"></span>

        <address id="7ztzv"></address>

            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???????????????????????????????p??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????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????????????????????????????????????????????????????E????????????????????????????????????????????????????????????????????????????????????????????????????????__main__????????????????????????????????????????????????????????????????????????__builtin__????

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

            ?????????Python???????????????????????“??????”???????????????????????????????????????????

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

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

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

            ??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????——?????Python?????????????????????????????“????”????????????????????????????????????????????????????????

            Python????????????????P????????????????????????????????????——??????????????????????????“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 ???????????????????????????????r???????? _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 ???????????????????a???????????

            >>> 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
            <span id="7ztzv"></span>
            <sub id="7ztzv"></sub>

            <span id="7ztzv"></span><form id="7ztzv"></form>

            <span id="7ztzv"></span>

                  <address id="7ztzv"></address>

                      ŷ