您的设计看起来像是在制作mixins。这是一个可以动态生成mixins的类工厂玩具示例。
figures.py
data = ((0,1),(0,3),4),(2,7),(6,3))
class FT:
@staticmethod
def ge():
return data
class FT1:
@staticmethod
def ge():
return [(x*x,y*y) for x,y in data]
def compose(ftype):
'''Returns a class composed of F and ftype.'''
return type(f'F_{ftype.__name__}',(F,ftype),{})
some_module.py:
import importlib
class F:
def __init__(self):
self.x = 'foo'
def a(self):
s = '|'.join(f'{thing}' for thing in self.ge())
return s
def b(self):
return 'baz'
def compose(ftype):
cls = getattr(importlib.import_module('figures'),ftype)
return type(f'F_{ftype}',cls),{})
z = compose('FT')()
y = compose('FT1')()
两个对象都具有相同的b
方法。
>>> z.b(),y.b()
('baz','baz')
两者都使用来自特定a
方法的数据的相同ge
方法
>>> print(z.a())
(0,1)|(0,3)|(0,4)|(2,1)|(2,3)|(2,7)|(6,3)
>>> y.a()
'(0,9)|(0,16)|(4,1)|(4,9)|(4,49)|(36,9)'
每个对象都有特定的ge
方法
>>> z.ge()
((0,3))
>>> y.ge()
[(0,9),16),(4,49),(36,9)]
>>>
z
和y
是不同类的实例。
>>> z.__class__,y.__class__
(<class '__main__.F_FT'>,<class '__main__.F_FT1'>)
>>>
组成类的多个实例。
>>> One = compose(FT)
>>> q,r,s = One(),One(),One()
>>> q,s
(<__main__.F_FT object at 0x0000000003163860>,<__main__.F_FT object at 0x000000000D334198>,<__main__.F_FT object at 0x000000000D334828>)
>>>
如果一切在同一模块中,则compose变为
def compose(ftype):
return type(f'F_{ftype.__name__}',{})
z = compose(FT)
y = compose(FT1)
What is the difference between a mixin and inheritance?
使用抽象基类的类似解决方案。 -除非实例化G,否则
ge
被覆盖。
import importlib
import abc
class G(abc.ABC):
def __init__(self):
self.x = 'foo'
def a(self):
s = '|'.join(f'{thing}' for thing in self.ge())
return s
def b(self):
return 'baz'
@staticmethod
@abc.abstractmethod
def ge():
pass
# either of these work
def new(ftype):
cls = getattr(importlib.import_module('figures'),ftype)
return type(cls.__name__,(cls,G),{})
#def new(ftype):
# cls = getattr(importlib.import_module('figures'),ftype)
# return type(cls.__name__,(G,),{'ge':staticmethod(cls.ge)})
#usage
# A = new('FT')
与其他类似,除了特定的 static 方法只是普通函数,并且从上方使用ABC
def FT_ge():
return ((0,3))
def other_new(f):
return type(f.__name__.split('_')[0],{'ge':staticmethod(f)})
# usage
# B = other_new(FT_ge)
,
您可以使用importlib.import_module
导入模块。但是,建议从基类继承这些类,该基类使用元类来跟踪其子类并在字典中进行映射。然后,您可以将此基类用作抽象来与所有子类Track subclasses in python
进行交互
本文链接:https://www.f2er.com/3132741.html