Last active
July 9, 2018 14:09
-
-
Save FGtatsuro/1f5e90059672fca7d550bb13d775c519 to your computer and use it in GitHub Desktop.
__init_subclass__ でメタクラスを置きかえる際に使えそうな属性
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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