Sorry, to ask yet another Python question. But because of the nature of it, I wanted to ask here as I don't get it.
From what I have read, attrs defined above an init statement in a class, are shared variables for all instances of a class. i mind meaning there is only one copy of these vars for all instances of the object. Well that's how I think it works.
But I often want to say have a constant in my class, let's say for a menu_bar_height. I really don't like putting these constants up the top of the file. Especially when the file starts growing and you start having a lot of constants.
Also, makes it harder, to pick up one class and move it into another file or just copy and re-purpose the class.
But by being defined in the class a level of association is implied, helping with naming etc.
The other thing, I think seems that would be beneficial, is when you come back some weeks or months later, it's a lot easier to understand the class and it's constants dependencies.
So I put a sample below. I just wanted to ask is it valid and ok to do it. The reason I ask, I see no code like this. Maybe it's around, I just haven't seen it. If this is fine, I would prefer to start using this method keeping in mind I have to understand, if I do change one of these attrs, they change for instances of the object.
class CodeDisplay(ui.View):
_label_name = 'style_label'
_tool_button_height = 36
_tool_bar_bg_color = 'lime'
_tool_bar_tint_color = 'black'
def __init__(self, style = 'colorful', use_linenos = True,
*args, **kwargs):
ui.View.__init__(self, *args, **kwargs)
self.code = None
self.web_view = None
self.style = style
self.use_linenos = use_linenos