You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
The text was updated successfully, but these errors were encountered:
@dhirschfeld You are probably right that there are multiple schools of thought on this issue. Thanks for the pointer to the IPython dev docs. I'll think through this a bit more and see if I can find other examples from popular open source projects.
I kindly disagree. While there are some special cases (which should be listed), it doesn’t make sense to do this. I have these reasons:
You have to type the variable name twice, which is unpythonic.
Having the variable at class-level implies that the variable has class-level use. Having the class-level always be None is confusing and not right. If it should never be referenced at class-level, it should not be at the class-level, only the instance level.
As for your argument 1, because __init__ is usually at the top, it doesn’t seem pythonic to do it again.
As for your argument 2, don’t use comments, that isn’t right. Use a docstring instead.
However, I do see your point of argument 3, so it should probably be put in a side note.
Also, to the question title “To repeat or not to repeat?”, the answer is to not repeat. If one has to repeat anything in their code, that is a problem and can be changed to not have the repeatition.
The current advice as given is:
I actually prefer to define all the attributes on the class itself for several reasons:
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
The text was updated successfully, but these errors were encountered: