0

Assuming I want to initialize every member variable in __init__, is there a recommended approach or style according to PEP? Especially for classes where the constructor also calls methods and has a bunch of member variables, I currently use the first style (A):

    class A(object):
        def __init__(self):
            self.a, self.b, self.c = [None] * 3
     
            self._populate_members()


    class B(object):
        def __init__(self):
            self.a = None
            self.b = None
            self.c = None

            self._populate_members()

Is there a PEP rule/recommendation about this?

mm rnm
  • 47
  • 4
  • This is a matter of opinion and therefore probably not considered a valid comment but here goes anyway... Ask yourself this - What if, for some reason during future development/maintenance, the initial value of self.b needs to be something other than None. Think about how the code would need to be changed and I think you'll have the answer – DarkKnight Jan 20 '22 at 15:59
  • @SergeBallesta The question asks about *what people prefer* and only incidentally asks if PEP says something about it. It doesn't ask if a chosen methodology is compliant with published PEP recommendation/standards. – TylerH Jan 20 '22 at 18:06

0 Answers0