gpt4 book ai didi

python - 在 Python 中创建参数化类型,但将所有实例设为 "super-type"的子实例

转载 作者:太空狗 更新时间:2023-10-30 00:08:50 25 4
gpt4 key购买 nike

假设我有一个具有 t 属性的 Python 类型。我想创建一个“参数化元类型”,以便执行以下操作:

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)

目前我收到以下错误:

---------------------------------------------------------------------------
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

最佳答案

可以满足第一部分或要求。但它需要一个辅助检查器类。 MySubClass 是类型的后代,MySubClass(0) 应该是一个类。它足以在 MySubClass 中创建一个内部类 InstanceChecker 类,并将 __instancecheck__ 覆盖它们。

代码可以是:

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))

顺便说一句,我已经删除了 __subclasscheck__ 覆盖,因为 t 仅在 MyObjectinstance 属性中/p>


或者,元类可以在bases 参数中自动添加父类(super class)。在下面的代码中,MySuperClass 不再是 MySubClass 的父类(super class),而是 MyObject 的父类(super class):

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)

关于python - 在 Python 中创建参数化类型,但将所有实例设为 "super-type"的子实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54967993/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com