Skip to content

To repeat or not to repeat? #34

Open
@dhirschfeld

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:

  1. 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
  2. The attributes can be (briefly) documented with comments to help understanding the api
  3. 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions