Skip to content

Instantly share code, notes, and snippets.

@pikhovkin
Created September 27, 2018 20:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pikhovkin/26162b7903fd03f56948319940dbd187 to your computer and use it in GitHub Desktop.
Save pikhovkin/26162b7903fd03f56948319940dbd187 to your computer and use it in GitHub Desktop.
Metaclass resolver
def metaclass_resolver(*classes, **options):
"""Metaclass resolver
Example:
SomeClass(metaclass_resolver(ClassMixin, BaseClass, SomeActionClass, some_field1=True, some_field2=42)):
pass
:param classes:
:param options:
:return:
"""
metaclass = tuple({t for t in set(map(type, classes)) if t.__name__ != 'type'})
if len(metaclass) == 1:
metaclass = metaclass[0]
else:
metaclass = type('_'.join(meta_cls.__name__ for meta_cls in metaclass), metaclass, options)
return metaclass('_'.join(cls.__name__ for cls in classes), classes, options)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment