python init subclass

Solutions on MaxInterview for python init subclass by the best coders in the world

showing results for - "python init subclass "
María Alejandra
16 Feb 2020
1class Philosopher:
2    def __init_subclass__(cls, default_name, **kwargs):
3        super().__init_subclass__(**kwargs)
4        print(f"Called __init_subclass({cls}, {default_name})")
5        cls.default_name = default_name
6
7class AustralianPhilosopher(Philosopher, default_name="Bruce"):
8    pass
9
10class GermanPhilosopher(Philosopher, default_name="Nietzsche"):
11    default_name = "Hegel"
12    print("Set name to Hegel")
13
14Bruce = AustralianPhilosopher()
15Mistery = GermanPhilosopher()
16print(Bruce.default_name)
17print(Mistery.default_name)
18