Class vs. Instance Variables

Issue #53 resolved
therealmeyerd NA created an issue

I saw that in a lot of classes, the parameters for those classes (e.g. in OMPTD maxBatchDiscovery etc.) are implemented as class variables and initialized with a default value. Then in the constructor, those variables are overwritten with the parameters given there (which themselves again have default values). Isn't this redundant?

Another problem could come up, since class variables are shared across all instances, that if we have multiple instance of one class, then weird effects could happen. Is this on purpose? Maybe this never was an issue, since most of the classes only get instanced once during an experiment ... but what about parallel execution of multiple experiments?

Comments (2)

  1. Jannik Schürg

    As long as they are immutable or constants (e.g. many models have a state_limits class variable, which is never changed), sharing between instances is no problem from a technical perspective, it might even save some space (although negligible).

    Overwriting the class variable in __init__ with an instance variable with the same name actually shadows the class variable. There are reasons why using instance variables from the start is a good policy, some are listed here at this question on stackexchange addressing this exact issue.

    I would say it's legitimate to use class variables for constants.

    Btw, documenting instance variables is also possible with sphinx.

  2. Log in to comment