
8

Python标准库: abc模块——抽象类和抽象方法的实现
source link: https://sineatos.github.io/tools/programme-language/python/stdlib/abc/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Python标准库: abc模块——抽象类和抽象方法的实现
由于python 没有抽象类、接口的概念,所以要实现这种功能得abc.py 这个类库,具体方式如下
import abc
class Foo(metaclass=abc.ABCMeta): ##抽象类
def f1(self):
print(123)
def f2(self):
print(456)
@abc.abstractmethod ##抽象方法
def f3(self):
'''
???
:return:
'''
class Bar(Foo):
def f3(self):
print(33333)
b = Bar()
b.f3()
依赖注入实现
引入依赖注入
解释器解释类的流程
# ======================================解释器解释类的流程===================
# 解释器解释:
# class Foo:
# def __init__(self):
# self.name =123
# def f1(self):
# print(self.name)
# 1.遇到class Foo,执行type的__init__方法
# 2.type的init的方法做什么呢!不知道
# obj =Foo()
# obj.f1()
# 3.执行Type的__call__方法
# 执行Foo类的__new__方法
# 执行Foo类的__init__方法
依赖注入在什么之前做什么操作
class MyType(type):
def __call__(cls, *args, **kwargs): ##执行Type的__call__方法,这里的cls就是<__main__.Foo object at 0x001B59F0> Foo类
obj = cls.__new__(cls, *args, **kwargs) ##Foo的__new__方法
print('-------------')
obj.__init__(*args, **kwargs) ##在执行Foo的__init__的之前做什么操作
return obj
class Foo(metaclass=MyType):
def __init__(self, name):
print('============')
self.name = name
def f1(self):
print(self.name)
obj = Foo(123)
print(obj)
print(obj.name)
#=================================依赖注入案例一======================================
class MyType(type):
def __call__(cls, *args, **kwargs): ##执行Type的__call__方法,这里的cls就是<__main__.Foo object at 0x001B59F0> Foo类
obj = cls.__new__(cls, *args, **kwargs) ##Foo的__new__方法
if cls == Foo1:
obj.__init__(Foo())
elif cls == Foo2:
obj.__init__(Foo1())
return obj
class Foo(metaclass=MyType):
def __init__(self, args):
print('============')
self.name = args
def f(self):
print(self.name)
class Foo1(metaclass=MyType):
def __init__(self, args):
print('============')
self.name = args
def f1(self):
print(self.name)
class Foo2(metaclass=MyType):
def __init__(self, args):
print('============')
self.name = args
def f2(self):
print(self.name)
obj = Foo2()
obj.f2()
# <__main__.Foo1 object at 0x002DA4F0>
#######################依赖注入案例二====================================================
#
# 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 value(cls):
# return Mapper.__mapper_relation[cls]
#
#
# class MyType(type):
# def __call__(cls, *args, **kwargs): ##执行Type的__call__方法,这里的cls就是<__main__.Foo object at 0x001B59F0> Foo类
# obj = cls.__new__(cls, *args, **kwargs) ##Foo的__new__方法
# arg_list = list(args)
# if Mapper.exist(cls):
# value = Mapper.value(cls)
# arg_list.append(value)
# obj.__init__(*arg_list, **kwargs) ##在执行Foo的__init__的之前做什么操作
# return obj
#
#
# class Foo(metaclass=MyType):
# def __init__(self, name):
# self.name = name
#
# def f1(self):
# print(self.name)
#
#
# class Bar(metaclass=MyType):
# def __init__(self, name):
# self.name = name
#
# def f1(self):
# print(self.name)
#
#
# Mapper.register(Foo, '666')
# Mapper.register(Bar, '999')
# obj = Foo()
#
# print(obj)
# print(obj.name)
# b = Bar()
# print(b.name)
# <__main__.Foo object at 0x00583810>
# 666
# 999
</article
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK