Skip to content

Commit b82183b

Browse files
DudeNr33jacobtylerwallsPierre-Sassoulas
authored
Add example for bad-super-call (#6166)
Co-authored by: Vladyslav Krylasov <vladyslav.krylasov@gmail.com> Co-authored-by: Jacob Walls <jacobtylerwalls@gmail.com> Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
1 parent 6718502 commit b82183b

4 files changed

Lines changed: 19 additions & 0 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Animal:
2+
pass
3+
4+
5+
class Cat(Animal):
6+
def __init__(self):
7+
super(Animal, self).__init__() # [bad-super-call]
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
In Python 2.7, ``super()`` has to be called with its own class and ``self`` as arguments (``super(Cat, self)``), which can
2+
lead to a mix up of parent and child class in the code.
3+
4+
In Python 3 the recommended way is to call ``super()`` without arguments (see also ``super-with-arguments``).
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Animal:
2+
pass
3+
4+
5+
class Cat(Animal):
6+
def __init__(self):
7+
super().__init__()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- `Documentation for super() <https://docs.python.org/3/library/functions.html#super>`_

0 commit comments

Comments
 (0)