According to Pycharm (and thus assume according by PEP) i should state all instance attributes directly inside __init__
. In my case it does not seem suitable to me. I have something like:
class Handler(object):
def __init__(self):
self.start_index = # parse
# Do stuff
def parse(self):
parse_header()
# Do stuff
parse_body()
def parse_header():
raise NotImplementedError
def parse_body():
raise NotImplementedError
class StringHandler(Handler):
def parse_header():
self.string_index = # parse
self.string_count = # parse
self.style_index = # parse
self.style_count = # parse
# More here
def parse_body():
self.strings = # parse
self.styles = # parse
# More here
class IntHandler(Handler):
def parse_header():
self.int_index = # parse
self.int_count = # parse
# More here
def parse_body():
self.ints = # parse
# More here
Given that there are objects (quite a lot of them) that all derive from Handler
class, the structure of each object it quite obvious for anyone who reads the code. To me it seems like unnecessary cluttering to declare __init__
just for sake of calling parent __init__
and declare couple of parameters as None
. Usually parse
-ing functions just parse whatever is needed and store that to attributes, thus adding such __init__
would sometimes almost double the size of the class (code-wise).
edit: I searched for this problem, but I did not found suitable answer, for example while searching stackexcahnge, the questions were either too broad to get useful answer ( How would you know if you've written readable and easily maintainable code? ) or asking different question ( Is it a good practice to declare instance variables as None in a class in Python? )