title: Ruby's tap idiom in Python
body: <p>There is a useful Ruby idiom that uses <code>tap</code> which allows you to create an object, do some operations on it and return it (I use a list here only as an example, my real code is more involved):</p>
  
  <pre><code>def foo
    [].tap do |a|
      b = 1 + 2
      # ... and some more processing, maybe some logging, etc.
      a &lt;&lt; b
    end
  end
  
  &gt;&gt; foo
  =&gt; [1]
  </code></pre>
  
  <p>With Rails there's a similar method called <code>returning</code>, so you can write:</p>
  
  <pre><code>def foo
    returning([]) do |a|
      b = 1 + 2
      # ... and some more processing, maybe some logging, etc.
      a &lt;&lt; b
    end
  end
  </code></pre>
  
  <p>which speaks for itself. No matter how much processing you do on the object, it's still clear that it's the return value of the function.</p>
  
  <p>In Python I have to write:</p>
  
  <pre><code>def foo():
    a = []
    b = 1 + 2
    # ... and some more processing, maybe some logging, etc.
    a.append(b)
    return a
  </code></pre>
  
  <p>and I wonder if there is a way to port this Ruby idiom into Python. My first thought was to use <code>with</code> statement, but <code>return with</code> is not valid syntax.</p>