[英]Create a parameterised type in Python, but make all instances children of the “super-type”
Suppose I have a Python type with the t
property. 假设我有一个带有
t
属性的Python类型。 I want to create a "parameterised metatype" such that the following works: 我想创建一个“参数化元类型”,以便以下工作:
class MySuperClass(type):
pass
class MySubClass(MySuperClass):
# Here is the problem -- How do I define types that contain stuff,
# independent of an object?
def __init__(self, t): # Or __getitem__
self.t = t
def __instancecheck__(self, instance):
return isinstance(instance, MySubClass) and instance.t == self.t
def __subclasscheck__(self, subclass):
return MySubClass in subclass.__mro__ and subclass.t == self.t
class MyObject(metaclass=MySubClass):
def __init__(self, t):
self.t = t
# Test code:
## Both of these, square brackets work too
assert isinstance(MyObject(0), MySubClass(0))
assert not isinstance(MyObject(0), MySubClass(1))
## Ideally
assert isinstance(MyObject(0), MySuperClass) or isinstance(MyObject(0), MySubClass)
Currently I get the following error: 目前我收到以下错误:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-4-99ad08881526> in <module>
14 return MySubClass in subclass.__mro__ and subclass.t == self.t
15
---> 16 class MyObject(metaclass=MySubClass):
17 def __init__(self, t):
18 self.t = t
TypeError: __init__() takes 2 positional arguments but 4 were given
It is possible to meet the first part or requirement. 可以满足第一部分或要求。 But it will require an auxilliary checker class.
但它需要辅助检查员课程。 A MySubClass is a descendant of type,
MySubClass(0)
should be a class. MySubClass是类型的后代,
MySubClass(0)
应该是一个类。 Its is enough to create an internal class InstanceChecker
class in MySubClass
, and put the __instancecheck__
override their. 它足以在
MySubClass
创建一个内部类InstanceChecker
类,并将__instancecheck__
覆盖它们。
Code could be: 代码可以是:
class MySubClass(MySuperClass):
def __new__(cls, name, bases=None, namespace=None, *args, **kwargs):
if bases is not None:
return super().__new__(cls, name, bases, namespace, **kwargs)
return cls.InstanceChecker(name)
class InstanceChecker:
def __init__(self, t):
self.t = t
def __instancecheck__(self, instance):
return isinstance(instance.__class__, MySubClass) and instance.t == self.t
class MyObject(metaclass=MySubClass):
def __init__(self, t):
self.t = t
# Test code:
## Both of these, square brackets work too
assert isinstance(MyObject(0), MySubClass(0))
assert not isinstance(MyObject(0), MySubClass(1))
BTW, I have removed the __subclasscheck__
override, because t
in only an instance attribute in MyObject
顺便说一句,我已经删除了
__subclasscheck__
覆盖,因为t
只在MyObject
一个实例属性中
Alternatively, the metaclass can automatically add a super class in the bases
parameter. 或者,元类可以在
bases
参数中自动添加超类。 In the following code, MySuperClass
is no longer a superclass of MySubClass
but of MyObject
: 在下面的代码中,
MySuperClass
不再是MySubClass
的超类,而是MyObject
的超类:
class MySuperClass():
pass
class MySubClass(type):
def __new__(cls, name, bases=None, namespace=None, *args, **kwargs):
if bases is not None:
return super().__new__(cls, name, bases + (MySuperClass,), namespace, **kwargs)
return cls.InstanceChecker(name)
class InstanceChecker:
def __init__(self, t):
self.t = t
def __instancecheck__(self, instance):
return isinstance(instance.__class__, MySubClass) and instance.t == self.t
class MyObject(metaclass=MySubClass):
def __init__(self, t):
self.t = t
# Test code:
## Both of these, square brackets work too
assert isinstance(MyObject(0), MySubClass(0))
assert not isinstance(MyObject(0), MySubClass(1))
## Ideally
assert isinstance(MyObject(0), MySuperClass)
I seem to have found a solution, and it is much, much cleaner than the one I originally had in mind. 我似乎找到了一个解决方案,它比我原先想到的更清洁。
class MyMetaSuper(type):
pass
class MyObject:
def __init__(self, t):
self.t = t
def __class_getitem__(cls, key):
class MyMeta(MyMetaSuper):
t = key
def __instancecheck__(self, instance):
return isinstance(instance, cls) and self.t == instance.t
def __subclasscheck__(self, subclass):
return isinstance(subclass, MyMetaSuper) and self.t == subclass.t
class MyObjectSpecific(MyObject, metaclass=MyMeta):
pass
return MyObjectSpecific
# Check for specific condition
assert isinstance(MyObject(0), MyObject[0])
# Make sure isinstance fails when condition fails
assert not isinstance(MyObject(0), MyObject[1])
# Test the generic object
assert isinstance(MyObject(0), MyObject)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.