Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to make a class that acts like a string?
    primarykey
    data
    text
    <p>I have a context manager that captures output to a string for a block of code indented under a <code>with</code> statement. This context manager yields a custom result object which will, when the block has finished executing, contain the captured output.</p> <pre><code>from contextlib import contextmanager @contextmanager def capturing(): "Captures output within a 'with' block." from cStringIO import StringIO class result(object): def __init__(self): self._result = None def __str__(self): return self._result try: stringio = StringIO() out, err, sys.stdout, sys.stderr = sys.stdout, sys.stderr, stringio, stringio output = result() yield output finally: output._result, sys.stdout, sys.stderr = stringio.getvalue(), out, err stringio.close() with capturing() as text: print "foo bar baz", print str(text) # prints "foo bar baz" </code></pre> <p>I can't just return a string, of course, because strings are immutable and thus the one the user gets back from the <code>with</code> statement can't be changed after their block of code runs. However, it is something of a drag to have to explicitly convert the result object to a string after the fact with <code>str</code> (I also played with making the object callable as a bit of syntactic sugar). </p> <p>So is it possible to make the result instance act like a string, in that it does in fact return a string when named? I tried implementing <code>__get__</code>, but that appears to only work on attributes. Or is what I want to do not really possible?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload