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

  • 相关阅读:
    分享一个Fluent风格的邮件发送封装类
    写一个ActionFilter检测WebApi接口请求和响应
    一道有趣的面试题,小鸟和火车的问题
    Centos7 查看Mysql配置文件
    Centos7 grep命令简介
    Centos7 网络配置
    django之python3.4及以上连接mysql的一些问题记录
    NetCore log4net 集成以及配置日志信息不重复显示或者记录
    ionic3中关于Ionic ui component使用的一些总结
    ionic2升级到ionic3并打包APK
  • 原文地址:https://www.cnblogs.com/meloncodezhang/p/12631414.html
Copyright © 2011-2022 走看看