首先,我们必须明确的一点是:python里无接口类型,定义接口只是一个人为规定,在编程过程自我约束

  • python的类是可以写任意个方法的

  • 定义一个接口对继承类进行约束,接口里有什么方法,继承类就必须有什么方法,接口中不能任何功能代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Interface:
     
    def f1(self):
        '''
        to do something
        :return:
        '''
 
class Something(Interface):
     
    def f1(self):
        print('to do something...')
     
    def f2(self):
        print('to do other..')

  在其他的语言里,比如Java,继承类没有重写接口方法是会报错的,而在python里不会,就是因为python没这个类型,所以只是在我们编程过程的一个规定,以I开头的类视为接口

1
2
3
4
5
6
7
8
9
class IOrderRepository:
 
    def fetch_one_by(self,nid):
        raise Exception('子类中必须实现该方法')
 
class Something(IOrderRepository):
 
    def fet_one_by(self,nid):
        print('查查查数据....')

抽象类,抽象方法

  • 抽象类,可以说是类和接口的混合体,既可以定义常规方法,也可以约束子类的方法(抽象方法)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import abc
 
#抽象类
class Foo(metaclass=abc.ABCMeta):
 
    def f1(self):
        print('f1')
 
    #抽象方法
    @abc.abstractmethod
    def f2(self):
        '''
        打印f2
        '''
 
class Bar(Foo):
 
    def f2(self):
        print('f2')
 
    def f3(self):
        print('f3')
 
= Bar()
b.f1()
b.f2()
b.f3()

依赖注入

首先我们先看一个普通的类:

1
2
3
4
5
6
class Foo:
    def __init__(self):
        self.name = 'alex'
     
    def f1(self):
        print(self.name)
  • 首先要明确的是,在python里,一切事物皆为对象

  • 而所有的类都是对象,默认是由type创建

创建类的执行流程:

  • 遇到class关键词,执行type的__init__方法,创建Foo类这个对象

  • 遇实例化对象(obj=Foo()),执行type里的__call__方法

  1. 在call方法里调用Foo类的__new__方法(负责创建对象)
  2. 执行Foo类的__init__方法(初始化)

了解其中的原理,我们就可以在__call__里面大做文章啦

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class MyType(type):
 
    def __call__(cls,*args,**kwargs):
        obj = cls.__new__(cls,*args,**kwargs)
        print('在这里面..')
        print('==========================')
        print('来咬我呀')
        obj.__init__(*args,**kwargs)
        return obj
 
 
class Foo(metaclass=MyType):
 
    def __init__(self):
        self.name = 'alex'
 
= Foo()
print(f.name)

  如果要熟练应用依赖注入,我还要弄懂一个概念,那就是组合:组合的目的就是解耦,减少依赖性,原来以某个具体的值或对象传入到内部改成以参数的形式传入

  比如:在实例Bar对象时,封装Foo对象,实例Foo对象封装Head对象,就用参数的形式传入到构造方法里

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
class Mapper:
 
    #在字典里定义依赖注入关系
    __mapper_relation = {}
 
    #类直接调用注册关系
    @staticmethod
    def register(cls,value):
        Mapper.__mapper_relation[cls= value
 
    @staticmethod
    def exist(cls):
        if cls in Mapper.__mapper_relation:
            return True
        return False
 
    @staticmethod
    def get_value(cls):
        return Mapper.__mapper_relation[cls]
 
class MyType(type):
    def __call__(cls,*args,**kwargs):
        obj = cls.__new__(cls,*args,**kwargs)
        arg_list = list(args)
        if Mapper.exist(cls):
            value = Mapper.get_value(cls)
            arg_list.append(value)
        obj.__init__(*arg_list,**kwargs)
        return obj
 
class Head:
 
    def __init__(self):
        self.name = 'alex'
 
class Foo(metaclass=MyType):
 
    def __init__(self,h):
        self.h = h
 
    def f1(self):
        print(self.h)
 
class Bar(metaclass=MyType):
 
    def __init__(self,f):
        self.f = f
 
    def f2(self):
        print(self.f)
 
Mapper.register(Foo,Head())
Mapper.register(Bar,Foo())
 
 
= Bar()
print(b.f)

python之接口的更多相关文章

  1. python+request接口自动化框架

    python+request接口自动化框架搭建 1.数据准备2.用python获取Excel文件中测试用例数据3.通过requests测试接口4.根据接口返回的code值和Excel对比 但本章只讲整 ...

  2. python版接口自动化测试框架源码完整版(requests + unittest)

    python版接口自动化测试框架:https://gitee.com/UncleYong/my_rf [框架目录结构介绍] bin: 可执行文件,程序入口 conf: 配置文件 core: 核心文件 ...

  3. python编写接口初识一

    python编写接口这里用到的是他一个比较轻量级的框架 flask #!/usr/bin/python # -*- coding: UTF-8 -*- import flask,json server ...

  4. Python Mongodb接口

    Python Mongodb接口 MongoDB 是一个基于分布式文件存储的数据库.由 C++ 语言编写.旨在为 WEB 应用提供可扩展的高性能数据存储解决方案. 同时,MongoDB 是一个介于关系 ...

  5. python+requests接口自动化测试

    转自https://my.oschina.net/u/3041656/blog/820023 原来的web页面功能测试转变成接口测试,之前大多都是手工进行,利用postman和jmeter进行的接口测 ...

  6. python+requests接口自动化测试框架实例详解

    python+requests接口自动化测试框架实例详解   转自https://my.oschina.net/u/3041656/blog/820023 摘要: python + requests实 ...

  7. 初识Django —Python API接口编程入门

    初识Django —Python API接口编程入门 一.WEB架构的简单介绍 Django是什么? Django是一个开放源代码的Web应用框架,由Python写成.我们的目标是用Python语言, ...

  8. python的接口类的思考?

    1.java怎么实现多继承的功效:https://www.cnblogs.com/Berryxiong/p/6142735.html 2.python的接口类和抽象类:https://www.cnbl ...

  9. python定义接口继承类

    zxq547 python定义接口继承类invalid syntax解决办法 1 2 3 4 5 6 7 class s_all(metaclass=abc.ABCMeta):     #python ...

  10. python+requests接口自动化框架

    为什么要做接口自动化框架 1.业务与配置的分离 2.数据与程序的分离:数据的变更不影响程序 3.有日志功能,实现无人值守 4.自动发送测试报告 5.不懂编程的测试人员也可以进行测试 正常接口测试的流程 ...

随机推荐

  1. ubuntu-12.04.5-desktop-i386.iso:ubuntu-12.04.5-desktop-i386:安装Oracle11gR2

    ubuntu 桌面版的安装不介绍. 如何安装oracle:核心步骤和关键点. ln -sf /bin/bash /bin/sh ln -sf /usr/bin/basename /bin/basena ...

  2. 【转载】高可用的MongoDB集群详解

    1.序言 MongoDB 是一个可扩展的高性能,开源,模式自由,面向文档的数据库. 它使用 C++编写.MongoDB 包含一下特点: l  面向集合的存储:适合存储对象及JSON形式的数据. l ...

  3. ubuntu alsa

    今天要在linux下搞音频编程,在网上查阅了一下资料,网上很多资料都是在linux下直接对/dev/dsp进行编程的,因为在以往的linux系统中,我们是可以通过cat  xxx.wav /dev/d ...

  4. [Scikit-learn] 2.5 Dimensionality reduction - ICA

    理论学习: 独立成分分析ICA历史 Ref: Lecture 15 | Machine Learning (Stanford) - NG From: https://wenku.baidu.com/v ...

  5. linux-ubuntu14.04以下使用gdb出现的问题

    问题: (gdb) list 没有符号表被读取. 请使用 "file" 命令. 原因事实上说的比較清楚,可运行文件里没有符号表,为什么会没有符号表呢.由于符号表是在编译过程中使用的 ...

  6. PMP模拟考试-2

    1. Increasing resources on the critical path activities may not always shorten the length of the pro ...

  7. Centos6.3 下使用 Tomcat-6.0.43 非root用户 部署 生产环境 端口转发方式

    一.安装JDK环境 方法一. 官方下载链接 http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260 ...

  8. firefox 好用的插件

    firefox一直是各位渗透测试必备的利器,这里整理了34款Firefox插件和几款Chrome的插件,其中包含渗透测试.信息收集.代理.加密解密等功能. Firefox插件 1:Firebug Fi ...

  9. using 释放内存的写法

    using (FileStream fileStream = File.Open(fileName,FileMode.Open,FileAccess.Read,FileShare.ReadWrite) ...

  10. 微信小程序 禁止ios页面下拉下滑滚动 出现空白的情况

    项目需要做了一个图片拖动指定组件上删除,和排序的功能android测试正常, ios会出现拖动图片页面也跟着下滑的尴尬情况. 查文档下拉刷新配置默认是关闭的,后经查找文档发现在本页面page.json ...