title: How to avoid lazy property evaluation on passing as argument in python
body: <p>I have the following code for lazily calculating a value in python 3.5. I have also tried the <code>@cached_property</code> <a href="https://pypi.python.org/pypi/cached-property" rel="nofollow noreferrer">decorator</a> with the same result, so I will use this for simplicity.</p>
  
  <pre><code>class Foo:    
      @property
      def expensive_object(self):
          if not hasattr(self, "_expensive_object"):            
              print("Lengthy initialization routine")
              self._expensive_object = 2
          return self._expensive_object           
  </code></pre>
  
  <p>The problem is that it gets evaluated when I pass it as an argument to a function, even if it ends up not being used inside, as in this example:</p>
  
  <pre><code>def bar(some_parameter, another_parameter):
      if some_parameter != 10:
          print(some_parameter)
      else:
          print(another_parameter)
  </code></pre>
  
  <p>From the following output we see that it got evaluated just by being passed, but it was not strictly necessary, as the code did not try to use it.</p>
  
  <pre><code>In [23]: foo1 = Foo()
      ...: bar(3, foo1.expensive_object)
           Lengthy initialization routine
           3
  
  In [24]: bar(3, foo1.expensive_object)
           3
  </code></pre>
  
  <p>There are of situations in which my script could run without ever needing to evaluate, but it ends up doing it anyway because of such cases.
  It would also not be practical to factor out the parameter. I also use it in the <code>__init__</code> of composed member objects.</p>
  
  <p>If possible I would like to make the property even more lazy in that it should evaluate only when actually read.</p>