3

I have seen somewhere the use of the following syntax:

class MyClass : AnotherClass

What does this mean? Is this inheritance without the use of MyClass(AnotherClass):?

Definity
  • 691
  • 2
  • 11
  • 31
  • 2
    Some real example you see? – Geeocode Dec 12 '19 at 01:11
  • 1
    In other languages in would be used to specify the parent class (C++, C#). Because of how python is structured, a colon like this would mean that AnotherClass is in the body of the class, and that the class only has 1 line. Based on this it couldn't exist in python. – T Bell Dec 12 '19 at 01:22
  • @TBell So do you think someone meant to write `class MyClass(AnotherClass):` but just forgot the Python syntax? It'd help to see a real example; like if `MyClass` has more to its body and uses methods defined in `AnotherClass` on itself, that'd confirm. – wjandrea Jun 28 '23 at 18:50

1 Answers1

1

After the colon i.e. the Class definition comes the building element of the Class:

class ClassName:
    <statement-1>
    .
    .
    .
    <statement-N>

So AnotherClass means nothing here itself and does nothing with inheritances.

The syntax:

class MyClass : AnotherClass

is the same like:

class MyClass: 
    AnotherClass

It could be an instantiation in the form:

a = AnotherClass()
Geeocode
  • 5,705
  • 3
  • 20
  • 34