Change language

object .__ init_subclass__ in Python

Allows additional initialization of the inherited class.

object .__ init_subclass __ (cls)

-> None

cls - The inherited class to be initialized. 


Called whenever there is inheritance from the class in which this method is defined.
Using the method simplifies the execution of some types of tasks that were previously solved using metaclasses.

Note

The default method implementation does nothing. However, it raises TypeError if arguments are passed to it.


  class Base: 
"" "Base class that handles initialization of inheritors." ""
registry = []

def __init_subclass __ (cls, my_arg = ’some’):

super () .__ init_subclass __ ()

# For example, create a registry of all descendants.
cls.registry.append (cls)

# Output the argument. 
print (my_arg)


class Child1 (Base):
"" "The first class is the descendant." ""

class Child2 (Base, my_arg = ’two’):
" "" Second successor. Pay attention to the possibility of passing an argument to the initializer using the example of my_arg.
"" "

class SubChild1 (Child1, my_arg = ’sub’):
" "" And a nested successor. "" "


# print in the initializer will output:
# some
# two
# sub

# And now let’s see what has accumulated in the register of heirs:
print (Base.registry)
# ["class ’__main __. Child1’ & gt ;, "class’ __main__ .Child2’ & gt ;, "class ’__main __. SubChild1’"]

Note

Metaclasses are never passed into this method.

Shop

Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best laptop for development

$499+
Gifts for programmers

Best laptop for Cricut Maker

$299+
Gifts for programmers

Best laptop for hacking

$890
Gifts for programmers

Best laptop for Machine Learning

$699+
Gifts for programmers

Raspberry Pi robot kit

$150

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically