Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you mean, can the function act differently based on the return types the caller is expecting, the answer is no (bar seriously nasty bytecode inspection). In this case, you should provide two different iterators on your object, and write something like:</p> <pre><code>for item in something: # Default iterator: returns non-tuple objects do_something(item) for (item,key) in something.iter_pairs(): # iter_pairs returns different iterator do_something_else(item, key) </code></pre> <p>eg. see the dictionary object, which uses this pattern. <code>for key in mydict</code> iterates over the dictionary keys. <code>for k,v in mydict.iteritems()</code> iterates over (key, value) pairs.</p> <p><strong>[Edit]</strong> Just in case anyone wants to see what I mean by "seriously nasty bytecode inspection", here's a quick implementation:</p> <pre><code>import inspect, opcode def num_expected_results(): """Return the number of items the caller is expecting in a tuple. Returns None if a single value is expected, rather than a tuple. """ f = inspect.currentframe(2) code = map(ord, f.f_code.co_code) pos = f.f_lasti if code[pos] == opcode.opmap['GET_ITER']: pos += 1 # Skip this and the FOR_ITER if code[pos] &gt; opcode.EXTENDED_ARG: pos +=5 elif code[pos] &gt; opcode.HAVE_ARGUMENT: pos +=3 else: pos += 1 if code[pos] == opcode.opmap['UNPACK_SEQUENCE']: return code[pos+1] + (code[pos+2] &lt;&lt; 8) return None </code></pre> <p>Usable something like:</p> <pre><code>class MagicDict(dict): def __iter__(self): if num_expected_results() == 2: for k,v in self.iteritems(): yield k,v else: for k in self.iterkeys(): yield k d=MagicDict(foo=1, bar=2) print "Keys:" for key in d: print " ", key print "Values" for k,v in d: print " ",k,v </code></pre> <p><strong>Disclaimer:</strong> This is incredibly hacky, insanely bad practice, and <em>will</em> cause other programmers to hunt you down and kill you if they ever see it in real code. Only works on cpython (if that). <strong>Never</strong> use this in production code (or for that matter, probably any code).</p>
 

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