Skip to content

Files

Latest commit

 

History

History
36 lines (26 loc) · 1.02 KB

non-parent-init-called.md

File metadata and controls

36 lines (26 loc) · 1.02 KB

Pattern: __init__ call unrelated to direct ancestor

Issue: -

Description

Used when an __init__ method is called on a class which is not in the direct ancestors for the analyzed class. This may lead to confusion by readers. Review the class in question to make sure this functionality is intended.

Example of incorrect code:

class Felinae(object):
    def __init__(self, subfamily):
        self.subfamily = subfamily

# if Puma was a descendant this would be "class Puma(Felinae):"
class Puma(object):
    def __init__(self, subfamily, genus):
        self.genus = genus
        Felinae.__init__(self, subfamily)  # calling __init__ of non-direct base class

Example of correct code:

class Felinae(object):
    def __init__(self, subfamily):
        self.subfamily = subfamily

# Puma is now a descendant of Felinae
class Puma(Felinae):
    def __init__(self, subfamily, genus):
        self.genus = genus
        Felinae.__init__(self, subfamily)  # calling __init__ of non-direct base class