Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python __init_subclass__

In [16]: class Foo:
    ...:     def __init_subclass__(cls):
    ...:         print('ok')
    ...:         print(cls.att)

In [17]: class SubFoo(Foo):
    ...:     att = 1
    ...:     pass
    ...:
ok
1

# So: __init_subclass__ runs everytime that Foo is subclassed.
# Also: the argument `cls` of __init_subclass__ is the subclass.
Comment

python __init_subclass__

class Philosopher:
    def __init_subclass__(cls, default_name, **kwargs):
        super().__init_subclass__(**kwargs)
        print(f"Called __init_subclass({cls}, {default_name})")
        cls.default_name = default_name

class AustralianPhilosopher(Philosopher, default_name="Bruce"):
    pass

class GermanPhilosopher(Philosopher, default_name="Nietzsche"):
    default_name = "Hegel"
    print("Set name to Hegel")

Bruce = AustralianPhilosopher()
Mistery = GermanPhilosopher()
print(Bruce.default_name)
print(Mistery.default_name)
Comment

PREVIOUS NEXT
Code Example
Python :: how to clean environment python 
Python :: is there a way to skip the first loop on a for loop python 
Python :: python repeting timer 
Python :: how to get unique value of all columns in pandas 
Python :: find duplicates in python list 
Python :: python logging basicconfig stdout 
Python :: concat dataframes 
Python :: full form of rom 
Python :: make tkinter label and input 
Python :: argparse required arguments 
Python :: print in binary python 
Python :: py env 
Python :: train test split 
Python :: custom signal godot 
Python :: import excel python 
Python :: program count the number of occurrences of a letter in a string python 
Python :: check if host is reachable python 
Python :: python time library 
Python :: press key on python 
Python :: python f string 
Python :: read a file in python 
Python :: python program for swapping position of two numbers 
Python :: indentation levels in programming 
Python :: beautiful soup get class name 
Python :: django setup in windows 
Python :: pyauto gui save screenshot 
Python :: openpyxl load file 
Python :: pandas sep 
Python :: otp generation in python 
Python :: how to use pafy 
ADD CONTENT
Topic
Content
Source link
Name
4+4 =