title: Understanding how a private variable decorated with @property is used in Python
body: <p>Given below is my code snippet </p>
  
  <pre><code>class TestClass:
      def __init__(self,n1,n2):
          self.n1 = n1
          self.n2 = n2
          self._sum = None # WILL BE COMPUTED LATER ON
  
      @property
      def sum(self):
          if self._sum is None:
             self._sum = self.compute_sum()
          return self._sum
  
      def compute_sum(self):
          return(self.n1+self.n2)
  
  test_obj = TestClass(5,6)
  
  print(test_obj.sum)
  </code></pre>
  
  <p>This is just an example program illustrating the problem I'm facing.
  I have a class with a private variable _sum in which I would like to hold the value of the two numbers n1 and n2. </p>
  
  <p>The property sum can be used by the external world (so as to speak) to access the value of the sum without being able to modify its value. The reason why I have a private variable _sum is so that the value is computed once and stored for subsequent fetches.</p>
  
  <p>Just having @property would compute the value every single time someone tries to access the value of sum through an instance and I would like to avoid this given 
  the performance impact (keep in mind, this is just an example to illustrate the actual problem that I'm facing) </p>
  
  <p>Given my requirement to be able to sum to all external objects to use but at the same time not have it be computed every time it's called, is my implementation, design wise, correct ?</p>
  
  <p>It's also quite interesting that the value of _sum is computed even before the function is called, at least my IDE was able to do that, not sure how.</p>
  
  <p>In the screenshot seen below, we're on line 9 and compute_sum() hasn't been called, yet the value has already been computed. How come ? </p>
  
  <p><a href="https://i.stack.imgur.com/ckLLb.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ckLLb.png" alt="enter image description here"></a></p>