I'm not a complete newbie, but pretty new to Python . While working on the project today, I got an idea and thought about using " self "; about which I read in the past for some time, and I still cannot understand whether this is always necessary or not. My question concerns only instances of classes and instance parameters / variables. This question is not about class variables that affect all instances .
An example :
class C: def __init__(self, parent = None): super(C, self).__init__(parent) self.some_temp_var = AnotherClass() self.important_property = self.some_temp_var.bring_the_jewels() ins_c = C() print ins_c.important_property
In the above code, I use self before declaring variables ( some_temp_var and important_property ).
I will have to access ** important_property ** later from the outside (where the instance was created) and maybe even change it.
But I do not need access to the instance of AnotherClass () and / or the variable pointing to it ( some_temp_var ). I just need an instance of this class once, and I need to execute its bring_the_jewels method only once to populate the important_property value.
Should I use self before declaring this variable?
self.some_temp_var = .... self.important_property = ....
or maybe:
some_temp_var = .... self.important_property = ....
Thank you for your help.
Ps. I did my research a long way. Due to the lack of my knowledge of English and / or CS, I might not have found an existing duplicate, but I really searched and I searched a lot. Before calling this question “duplicate” or “not constructively”, please read it. This is a question with a clear answer, and it is very important, and a difficult question. Thank you for your understanding.
source share