本文整理匯總了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)