ПВ
Size: a a a
ПВ
E
E
Х
Х
*py
class meta(type):
def __new__(mcs, *args, **kwargs):
cls = super().__new__(mcs, *args, **kwargs)
cls.__bounded = None
return cls
def f(cls):
return cls.__bounded
p(meta().f())
b
Х
*py
class meta(type):
def __new__(mcs, *args, **kwargs):
cls = super().__new__(mcs, *args, **kwargs)
cls.__bounded = None
return cls
def f(cls):
return cls.__bounded
class b(metaclass=meta):
pass
p(b.f())
Х
.__bounded
в метаклассе ломает питонХ
Х
class RequestMeta(type):
def __getitem__(cls, flags):
if isinstance(flags, slice):
if slice.step is not None:
raise TypeError("Unexpected step value in slice")
request_flags = FlagSet(flags.start)
bot_flags = FlagSet(flags.stop)
else:
request_flags = FlagSet(flags)
bot_flags = None
if not isinstance(request_flags.flag_type, RequestFlag):
raise TypeError
if bot_flags is not None and not isinstance(bot_flags.flag_type, BotFlag):
raise TypeError
return request_flags, bot_flags
def __get__(cls, instance, owner):
a = cls.__bounded
return cls.__bounded(instance)
def __call__(cls, *args, **kwargs):
raise NotImplementedError
def __new__(mcs, name, bases, dct):
# if Request not in bases:
# bases += (Request,)
cls = super().__new__(mcs, name, bases, dct)
a = BoundedRequestMeta(name, (cls,), dict())
cls.__bounded = a
return cls
class BoundedRequestMeta(RequestMeta):
def __new__(cls, name, bases, dct):
return type.__new__(cls, name, bases, dct)
class Request(metaclass=RequestMeta):
pass
if __name__ == '__main__':
class TestClass:
class test_req(Request):
def __call__(self):
return self.__call()
def __call(self):
print("call")
def __geti(self):
print("get")
def __getitem__(self, flags):
return self.__geti
a = TestClass()
a.test_req()
a.test_req[Flag()]()