美文网首页
python的继承

python的继承

作者: shuff1e | 来源:发表于2018-03-27 23:06 被阅读21次
    [root@shuffle-dev py_test]$ vim sf_nameMangling.py 
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    class A(object):
        def __init__(self):
            self.__private()
            self.public()
        def __private(self):
            print 'A.private()'
        def public(self):
            print 'A.public'
    class B(A):
        def __private(self):
            print 'B.private()'
        def public(self):
            print 'B.public'
    class C(A):
        def __init__(self):
            self.__private()
            self.public()
        @classmethod
        def __private(cls):
            print 'C.private'
        @staticmethod
        def public():
            print 'public'
    
    print '---create b---'
    b=B()
    print '---create c---'
    c=C()
    print '---method of class C---'
    C._C__private()
    C.public()   
    
    [root@shuffle-dev py_test]$ vim sf_classextend.py
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    #Author:xushuffle
    import sys
    reload(sys)
    sys.setdefaultencoding('utf-8')
    
    from array import array
    
    class base(object):
        def __init__(self,x,y):
            self.x,self.y=x,y
        def echo(self):
            print self.x,self.y
    
    class sub(base):
        typecode='d'
        def __init__(self,components):
            self.components=array(typecode,components)
    
    # s=sub(range(5))
    # s.echo()
    
    class sub2(base):
        typecode='d'
        def __init__(self,components):
            self.x,self.y=components[0],components[1]
            self.components=array(self.typecode,components)
    
    s=sub2(range(5))                                                                                                                              
    s.echo()
    
    • python作为动态语言和java等静态语言的行为不太一致
    [root@shuffle-dev py_test]$ python
    Python 2.6.6 (r266:84292, Jul 10 2013, 22:48:45) 
    [GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from sf_classextend import sub
    0 1
    >>> from dis import dis
    >>> dis(sub)
    Disassembly of __init__:
     19           0 LOAD_GLOBAL              0 (array)
                  3 LOAD_GLOBAL              1 (typecode)
                  6 LOAD_FAST                1 (components)
                  9 CALL_FUNCTION            2
                 12 LOAD_FAST                0 (self)
                 15 STORE_ATTR               2 (components)
                 18 LOAD_CONST               0 (None)
                 21 RETURN_VALUE        
    
    >>> 
    

    dis模块的参考文档在这里:https://docs.python.org/2/library/dis.html
    typecode是从global里面load的
    例如
    LOAD_FAST(var_num)
    Pushes a reference to the local co_varnames[var_num] onto the stack.

    [root@shuffle-dev py_test]$ python
    Python 2.6.6 (r266:84292, Jul 10 2013, 22:48:45) 
    [GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> f=open('sf_classextend.py','r').read()
    >>> co=compile(f,'sf_classextend.sub','exec')
    >>> print co.co_varnames
    ()
    >>> print co.co_names
    ('sys', 'reload', 'setdefaultencoding', 'array', 'object', 'base', 'sub', 'sub2', 'range', 's', 'echo')
    >>> print co.co_consts
    (-1, None, 'utf-8', ('array',), 'base', <code object base at 0x2aaed06c64e0, file "sf_classextend.sub", line 10>, 'sub', <code object sub at 0x2aaed06cd120, file "sf_classextend.sub", line 16>, 'sub2', <code object sub2 at 0x2aaed06d7738, file "sf_classextend.sub", line 24>, 5)
    >>> 
    

    相关文章

      网友评论

          本文标题:python的继承

          本文链接:https://www.haomeiwen.com/subject/gljecftx.html