A
A
Alexander Bureyko2021-12-01 23:42:25
Python
Alexander Bureyko, 2021-12-01 23:42:25

Why include the parent class in parentheses when defining a child class?

Hello, I have two versions of the code.
first:

class A:
  def __init__(self):
    print('nice')
class B(A):
  def __init__(self):
    A.__init__(self)
v = B()

and the second version:
class A:
  def __init__(self):
    print('nice')
class B:
  def __init__(self):
    A.__init__(self)
v = B()

both work without errors, but then the question arises - why use the indication of the parent class in brackets?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2021-12-01
@alexander1212

To inherit the properties of the parent class. Add fields and methods to A, the difference will become noticeable.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question