In Python (more specifically Python 3.x), what happens if I say x = x
, where x
is either a reference to a mutable (like list
) or a reference to an immutable (like int
) on the low-level? Does the compiler simply ignore such nonsense?
More specifically, what does the compiler do if we have the following case:
class A:
def __init__(self):
self.a = self.init_a()
def init_a(self):
self.a = some_value
"""
do stuff with self.a here
"""
return self.a
For those who haven't noticed, self.a
effectively gets assigned to itself through the function, init_a(self)
.
I know this case with class A
above seems silly, but I am trying to keep my code clean and readable by clearly initializing all my member variables inside the __init__(self)
function (in a different class that I am implementing for real). I just want to know for interest's sake whether the compiler optimizes that step away, or whether it does some operations in any case, even though the statement does not result in anything (I'm guessing it gets optimized away, but I want to be sure - you never know).