本文整理汇总了Python中types.resolve_bases方法的典型用法代码示例。如果您正苦于以下问题:Python types.resolve_bases方法的具体用法?Python types.resolve_bases怎么用?Python types.resolve_bases使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类types
的用法示例。
在下文中一共展示了types.resolve_bases方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: with_metaclass
# 需要导入模块: import types [as 别名]
# 或者: from types import resolve_bases [as 别名]
def with_metaclass(meta, *bases):
"""Create a base class with a metaclass."""
# This requires a bit of explanation: the basic idea is to make a dummy
# metaclass for one level of class instantiation that replaces itself with
# the actual metaclass.
class metaclass(type):
def __new__(cls, name, this_bases, d):
if sys.version_info[:2] >= (3, 7):
# This version introduced PEP 560 that requires a bit
# of extra care (we mimic what is done by __build_class__).
resolved_bases = types.resolve_bases(bases)
if resolved_bases is not bases:
d['__orig_bases__'] = bases
else:
resolved_bases = bases
return meta(name, resolved_bases, d)
@classmethod
def __prepare__(cls, name, this_bases):
return meta.__prepare__(name, bases)
return type.__new__(metaclass, 'temporary_class', (), {})
示例2: test_resolve_bases
# 需要导入模块: import types [as 别名]
# 或者: from types import resolve_bases [as 别名]
def test_resolve_bases(self):
class A: pass
class B: pass
class C:
def __mro_entries__(self, bases):
if A in bases:
return ()
return (A,)
c = C()
self.assertEqual(types.resolve_bases(()), ())
self.assertEqual(types.resolve_bases((c,)), (A,))
self.assertEqual(types.resolve_bases((C,)), (C,))
self.assertEqual(types.resolve_bases((A, C)), (A, C))
self.assertEqual(types.resolve_bases((c, A)), (A,))
self.assertEqual(types.resolve_bases((A, c)), (A,))
x = (A,)
y = (C,)
z = (A, C)
t = (A, C, B)
for bases in [x, y, z, t]:
self.assertIs(types.resolve_bases(bases), bases)