mardi 13 octobre 2020

Python state design for class state

I'm asking about best practices for design patterns in python controlling state with respect to class state.

The most common way I see state design patterns implemented is to instantiate an object and have methods/attribute setting to alter the objects state, affecting behaviour. I'm wondering whether its considered a fine and encouraged pattern or an anti pattern to extend the logic to the class level.

I tend to write a lot of class methods (maybe because of my past experience in Java). I often have classes which I never intend to instantiate but just house as collection of related methods and class attributes which those methods rely on. Recently, I've found examples where it would make more sense to me to alter class attributes to alter state over instantiating an object i.e. when I intend to only use that class in a singular given state throughout an application but that state is determined at runtime.

I could just instantiate the class and set the runtime attributes on initialisation, but this seems like overkill since I would only ever instantiate the class once.

What's the encouraged pythonic design in these cases?

Aucun commentaire:

Enregistrer un commentaire