Should I use "I" to define instanced variables / objects that I don’t need to get from the outside?

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.

+6
source share
3 answers

If you are not using self.some_temp_var , then you are defining a local variable that will be garbage collected after the constructor.

So you need self if you want to define the attribute of the instance that should be stored with the instance. If you want this public or private, this is a naming question. Prefix with _ for protected or __ for private.

If some_temp_var is just a temp for the constructor, and you don't need it anymore, then no, you don't need self . This applies to persistence and member attributes. You do not need self for the pure fact of working with a variable within a single method.

Consider this ... What if some_temp_var had some sort of huge data structure that you create to get the actual value of the result? You do not want to use self to store a temporary variable, because it will not free memory until it is deleted or instantiated. You want to be cleaned when it is no longer needed.

+7
source

It looks like you don't need a variable at all:

 class C: def __init__(self, parent=None): super(C, self).__init__(parent) self.important_property = AnotherClass().bring_the_jewels() 

In response to your comment:

I need to put myself. Infront it, if it is created, referenced and used once during __init__ .

No no. Then use a regular variable and the object will be garbage collected after __init__ exits.

 class C: def __init__(self, parent = None): super(C, self).__init__(parent) some_temp_var = AnotherClass() self.important_property = some_temp_var.bring_the_jewels() 
+4
source

Python does not distinguish "public" from "private" variables. Everything that you assign a name to self using self_something = value will be the instance variable for that instance.

Python has two conventions that are important here:

1) Python fields or methods are prefixed with underscores ('_') to tell users that this is not a problem outside this class. The only underline is the usual way of determining more or less that a language like C # will call a “protected” variable: it is internal to implement and should be left alone, except for this class and derived classes. Double underlining is similar to the C # 'private' variable: it is internal and should be left alone even in derived classes, it cannot be overridden

 class Example(object): def __init__(self) self._protected= "protected" self.__private = "private" self.public = "public" class Derived(Example): pass d = Derived() print d._protected >> protected print d.__private >> AttributeError: 'Derived' object has no attribute '__private' 

Strictly speaking, the only underline is just a typographic agreement. Double underscores really cripple a name inside a class to prevent inheritance.

2) For example, you can potentially use a property decorator if AnotherClass is actually going to hang, and if manipulating an instance of AnotherClass affects the state of AnotherClass.

 class C: def __init__(self, parent = None): super(C, self).__init__(parent) self._private = AnotherClass() @property def public(self): return self._private.some_method_on_AnotherClass() @public.setter def set_pub(self, val) self._private.set_value_on_AnotherClass(val) 
+3
source

All Articles