<p>In python, you can have a function return multiple values.  Here's a contrived example:</p>

<pre><code>def divide(x, y):
    quotient = x/y
    remainder = x % y
    return quotient, remainder  

(q, r) = divide(22, 7)
</code></pre>

<p>This seems very useful, but it looks like it can also be abused ("Well..function X already computes what we need as an intermediate value.  Let's have X return that value also").</p>

<p>When should you draw the line and define a different method?  </p>

[{'answer_id': 61629, 'author': 'Jason Etheridge', 'author_id': 2193, 'author_profile': 'https://Stackoverflow.com/users/2193', 'pm_score': 5, 'selected': False, 'text': "<p>Firstly, note that Python allows for the following (no need for the parenthesis):</p>\n\n<pre><code>q, r = divide(22, 7)\n</code></pre>\n\n<p>Regarding your question, there's no hard and fast rule either way. For simple (and usually contrived) examples, it may seem that it's always possible for a given function to have a single purpose, resulting in a single value. However, when using Python for real-world applications, you quickly run into many cases where returning multiple values is necessary, and results in cleaner code.</p>\n\n<p>So, I'd say do whatever makes sense, and don't try to conform to an artificial convention. Python supports multiple return values, so use it when appropriate.</p>\n"}, {'answer_id': 61636, 'author': 'jfs', 'author_id': 4279, 'author_profile': 'https://Stackoverflow.com/users/4279', 'pm_score': 8, 'selected': True, 'text': '<p>Absolutely (for the example you provided).</p>\n\n<h3>Tuples are first class citizens in Python</h3>\n\n<p>There is a builtin function <a href="https://docs.python.org/3/library/functions.html#divmod" rel="noreferrer"><code>divmod()</code></a> that does exactly that.</p>\n\n<pre><code>q, r = divmod(x, y) # ((x - x%y)/y, x%y) Invariant: div*y + mod == x\n</code></pre>\n\n<p>There are other examples: <code>zip</code>, <code>enumerate</code>, <code>dict.items</code>. </p>\n\n<pre><code>for i, e in enumerate([1, 3, 3]):\n    print "index=%d, element=%s" % (i, e)\n\n# reverse keys and values in a dictionary\nd = dict((v, k) for k, v in adict.items()) # or \nd = dict(zip(adict.values(), adict.keys()))\n</code></pre>\n\n<p>BTW, parentheses are not necessary most of the time.\nCitation from <a href="https://docs.python.org/3/library/stdtypes.html?highlight=type%20seq#tuple" rel="noreferrer">Python Library Reference</a>: </p>\n\n<blockquote>\n  <p>Tuples may be constructed in a number of ways:</p>\n  \n  <ul>\n  <li>Using a pair of parentheses to denote the empty tuple: ()</li>\n  <li>Using a trailing comma for a singleton tuple: a, or (a,)</li>\n  <li>Separating items with commas: a, b, c or (a, b, c)</li>\n  <li>Using the tuple() built-in: tuple() or tuple(iterable)</li>\n  </ul>\n</blockquote>\n\n<h3>Functions should serve single purpose</h3>\n\n<p>Therefore they should return a single object. In your case this object is a tuple. Consider tuple as an ad-hoc compound data structure. There are languages where almost every single function returns multiple values (list in Lisp).</p>\n\n<p>Sometimes it is sufficient to return <code>(x, y)</code> instead of <code>Point(x, y)</code>.</p>\n\n<h3>Named tuples</h3>\n\n<p>With the introduction of named tuples in Python 2.6 it is preferable in many cases to return named tuples instead of plain tuples.</p>\n\n<pre><code>&gt;&gt;&gt; import collections\n&gt;&gt;&gt; Point = collections.namedtuple(\'Point\', \'x y\')\n&gt;&gt;&gt; x, y = Point(0, 1)\n&gt;&gt;&gt; p = Point(x, y)\n&gt;&gt;&gt; x, y, p\n(0, 1, Point(x=0, y=1))\n&gt;&gt;&gt; p.x, p.y, p[0], p[1]\n(0, 1, 0, 1)\n&gt;&gt;&gt; for i in p:\n...   print(i)\n...\n0\n1\n</code></pre>\n'}, {'answer_id': 61637, 'author': 'Nathan Jones', 'author_id': 5848, 'author_profile': 'https://Stackoverflow.com/users/5848', 'pm_score': 4, 'selected': False, 'text': '<p>The example you give is actually a python builtin function, called <code>divmod</code>. So someone, at some point in time, thought that it was pythonic enough to include in the core functionality.</p>\n\n<p>To me, if it makes the code cleaner, it is pythonic. Compare these two code blocks:</p>\n\n<pre><code>seconds = 1234\nminutes, seconds = divmod(seconds, 60)\nhours, minutes = divmod(minutes, 60)\n\nseconds = 1234\nminutes = seconds / 60\nseconds = seconds % 60\nhours = minutes / 60\nminutes = minutes % 60\n</code></pre>\n'}, {'answer_id': 63528, 'author': 'Will Harris', 'author_id': 4702, 'author_profile': 'https://Stackoverflow.com/users/4702', 'pm_score': 0, 'selected': False, 'text': '<p>It\'s fine to return multiple values using a tuple for simple functions such as <code>divmod</code>. If it makes the code readable, it\'s Pythonic.</p>\n\n<p>If the return value starts to become confusing, check whether the function is doing too much and split it if it is. If a big tuple is being used like an object, make it an object. Also, consider using <a href="http://code.activestate.com/recipes/500261/" rel="nofollow noreferrer">named tuples</a>, which will be part of the standard library in Python 2.6.</p>\n'}, {'answer_id': 63549, 'author': 'indentation', 'author_id': 7706, 'author_profile': 'https://Stackoverflow.com/users/7706', 'pm_score': 1, 'selected': False, 'text': "<p>It's definitely pythonic. The fact that you can return multiple values from a function the boilerplate you would have in a language like C where you need to define a struct for every combination of types you return somewhere.</p>\n\n<p>However, if you reach the point where you are returning something crazy like 10 values from a single function, you should seriously consider bundling them in a class because at that point it gets unwieldy.</p>\n"}, {'answer_id': 63809, 'author': 'pixelbeat', 'author_id': 4421, 'author_profile': 'https://Stackoverflow.com/users/4421', 'pm_score': 1, 'selected': False, 'text': '<p>Returning a tuple is cool. Also note the new namedtuple\nwhich was added in python 2.6 which may make this more palatable for you:\n<a href="http://docs.python.org/dev/library/collections.html#collections.namedtuple" rel="nofollow noreferrer">http://docs.python.org/dev/library/collections.html#collections.namedtuple</a></p>\n'}, {'answer_id': 64110, 'author': 'zweiterlinde', 'author_id': 6592, 'author_profile': 'https://Stackoverflow.com/users/6592', 'pm_score': 2, 'selected': False, 'text': '<p>Yes, returning multiple values (i.e., a tuple) is definitely pythonic.  As others have pointed out, there are plenty of examples in the Python standard library, as well as in well-respected Python projects.  Two additional comments:</p>\n\n<ol>\n<li>Returning multiple values is sometimes very, very useful.  Take, for example, a method that optionally handles an event (returning some value in doing so) and also returns success or failure.  This might arise in a chain of responsibility pattern.  In other cases, you want to return multiple, closely linked pieces of data---as in the example given.  In this setting, returning multiple values is akin to returning a single instance of an anonymous class with several member variables.</li>\n<li><p>Python\'s handling of method arguments necessitates the ability to directly return multiple values.  In C++, for example, method arguments can be passed by reference, so you can assign output values to them, in addition to the formal return value.  In Python, arguments are passed "by reference" (but in the sense of Java, not C++).  You can\'t assign new values to method arguments and have it reflected outside method scope.  For example:</p>\n\n<pre><code>// C++\nvoid test(int&amp; arg)\n{\n    arg = 1;\n}\n\nint foo = 0;\ntest(foo); // foo is now 1!\n</code></pre>\n\n<p>Compare with:</p>\n\n<pre><code># Python\ndef test(arg):\n    arg = 1\n\nfoo = 0\ntest(foo) # foo is still 0\n</code></pre></li>\n</ol>\n'}, {'answer_id': 66967, 'author': 'Fred Larson', 'author_id': 10077, 'author_profile': 'https://Stackoverflow.com/users/10077', 'pm_score': 0, 'selected': False, 'text': "<p>I'm fairly new to Python, but the tuple technique seems very pythonic to me.  However, I've had another idea that may enhance readability.  Using a dictionary allows access to the different values by name rather than position.  For example:</p>\n\n<pre><code>def divide(x, y):\n    return {'quotient': x/y, 'remainder':x%y }\n\nanswer = divide(22, 7)\nprint answer['quotient']\nprint answer['remainder']\n</code></pre>\n"}, {'answer_id': 640632, 'author': 'NevilleDNZ', 'author_id': 77431, 'author_profile': 'https://Stackoverflow.com/users/77431', 'pm_score': 1, 'selected': False, 'text': '<p>OT: RSRE\'s Algol68 has the curious "/:=" operator. eg.</p>\n\n<pre><code>INT quotient:=355, remainder;\nremainder := (quotient /:= 113);\n</code></pre>\n\n<p>Giving a quotient of 3, and a remainder of 16. </p>\n\n<p>Note: typically the value of "(x/:=y)" is discarded as quotient "x" is assigned by reference, but in RSRE\'s case the returned value is the remainder.</p>\n\n<p>c.f. <a href="http://rosettacode.org/wiki/Basic_integer_arithmetic#ALGOL_68" rel="nofollow noreferrer">Integer Arithmetic - Algol68</a></p>\n'}]
['https://Stackoverflow.com/questions/61605', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4883/']