Skip to content

Instantly share code, notes, and snippets.

@FGtatsuro
Last active July 9, 2018 14:09
Show Gist options
  • Save FGtatsuro/1f5e90059672fca7d550bb13d775c519 to your computer and use it in GitHub Desktop.
Save FGtatsuro/1f5e90059672fca7d550bb13d775c519 to your computer and use it in GitHub Desktop.
__init_subclass__ でメタクラスを置きかえる際に使えそうな属性
class Piyo:
pass
# FYI: https://docs.python.jp/3/reference/datamodel.html#object.__init_subclass__
class Philosopher:
def __init_subclass__(cls, default_name, **kwargs):
# FYI: https://docs.python.jp/3/library/stdtypes.html#special-attributes
print(cls.hoge)
print(cls.__class__)
print(cls.__class__.__name__)
print(cls.__name__)
print(cls.__subclasses__())
print(cls.__mro__)
print(object in cls.__mro__)
print(Piyo in cls.__mro__)
# Philosopherの定義より後で定義されたクラスは使えない
# print(Hoge in cls.__mro__)
super().__init_subclass__(**kwargs)
cls.default_name = default_name
class AustralianPhilosopher(Philosopher, default_name="Bruce"):
hoge = 'test'
class Hoge:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment