Skip to content

Latest commit

 

History

History
27 lines (19 loc) · 685 Bytes

return-in-init.md

File metadata and controls

27 lines (19 loc) · 685 Bytes

Pattern: Returning a value from __init__

Issue: -

Description

Because __new__() and __init__() work together in constructing objects, no non-None value may be returned by __init__(); doing so will cause a TypeError to be raised at runtime.

Example of incorrect code:

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

Example of correct code:

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

Further Reading