zoukankan      html  css  js  c++  java
  • 继承之super

    super()方法:调用父类方法,也就是新建一个super类的实例对象

    class super(object):
        """
        super() -> same as super(__class__, <first argument>)
        super(type) -> unbound super object
        super(type, obj) -> bound super object; requires isinstance(obj, type)
        super(type, type2) -> bound super object; requires issubclass(type2, type)
        Typical use to call a cooperative superclass method:
        class C(B):
            def meth(self, arg):
                super().meth(arg)
        This works for class methods too:
        class C(B):
            @classmethod
            def cmeth(cls, arg):
                super().cmeth(arg)
        """
        def __getattribute__(self, *args, **kwargs): # real signature unknown
            """ Return getattr(self, name). """
            pass
    
        def __get__(self, *args, **kwargs): # real signature unknown
            """ Return an attribute of instance, which is of type owner. """
            pass
    
        def __init__(self, type1=None, type2=None): # known special case of super.__init__
            """
            super() -> same as super(__class__, <first argument>)
            super(type) -> unbound super object
            super(type, obj) -> bound super object; requires isinstance(obj, type)
            super(type, type2) -> bound super object; requires issubclass(type2, type)
            Typical use to call a cooperative superclass method:
            class C(B):
                def meth(self, arg):
                    super().meth(arg)
            This works for class methods too:
            class C(B):
                @classmethod
                def cmeth(cls, arg):
                    super().cmeth(arg)
            
            # (copied from class doc)
            """
            pass
    
        @staticmethod # known case of __new__
        def __new__(*args, **kwargs): # real signature unknown
            """ Create and return a new object.  See help(type) for accurate signature. """
            pass
    
        def __repr__(self, *args, **kwargs): # real signature unknown
            """ Return repr(self). """
            pass
    
        __self_class__ = property(lambda self: type(object))
        """the type of the instance invoking super(); may be None
    
        :type: type
        """
    
        __self__ = property(lambda self: type(object))
        """the instance invoking super(); may be None
    
        :type: type
        """
    
        __thisclass__ = property(lambda self: type(object))
        """the class invoking super()
    
        :type: type
        """

    举例

    class A(object):
    
        def A_method(self,p1):
            print(p1)
    
    
    class B(A):
    
        def B_method(self):
            # python3 写法
            super().A_method(10) 
            # python2 写法
            # super(B,self).A_method(10)
    
    b = B()
    b.B_method()
    
    # 结果
    10

    使用场景,封装flask的app,供外界使用

    # application.py

    from flask import Flask
    from flask_sqlalchemy import SQLALlchemy
    class Application(Flask):
        def __init__(self,import_name):
            super(Application,self).__init__(import_name)  # python2的写法,创建Application的实例时候,传入__name__,就相当于在当前文件创建了app=Flask(__name__)实例
         # 上面的super这一行执行完成之后,self已经变成了app self.config.from_pyfile(
    "config/base_setting.py") db.init_app(self)
    db = SQLALlchemy()
    app = Application(__name__)   # 外界任何位置想使用app和db的时候,直接 from application import app,db 就可以直接使用app,db

    # TOTO

  • 相关阅读:
    JDBC值事务
    JDBC之存储过程
    2016年计划要看的书,收集来自爱学习爱阅读的精英们
    step by step设置postgresql用户密码并配置远程连接
    在CentOS上搭建svn服务器及注意事项
    我为儿子开发的第一款Android App,用于九九乘法练习
    C语言实现九九乘法口诀表
    selenium-自动化测试51job网站(MacOS + Safari)2020年10月6日
    Oracle中row_number()、rank()、dense_rank() 的区别
    SQL错题集
  • 原文地址:https://www.cnblogs.com/meloncodezhang/p/12631414.html
Copyright © 2011-2022 走看看