Open
Description
The current advice as given is:
# bad
class JSONWriter(object):
handler = None
def __init__(self, handler):
self.handler = handler
# good
class JSONWriter(object):
def __init__(self, handler):
self.handler = handler
I actually prefer to define all the attributes on the class itself for several reasons:
- It defines the api in a single location at the top of the class - this is similar in principle to the advice to set all attributes in the constructor
- The attributes can be (briefly) documented with comments to help understanding the api
- You don't need to actually instantiate an instance of the class to use tab-completion to investigate the api. Sometimes for classes which take complicated object arguments, instantiating the class can be a non-trivial exercise (that may be a code smell itself, but it does happen)
This is the advice given in the ipython dev docs so perhaps the current advice could be supplemented with a note to the effect that there are different schools of thought on the matter
Activity