Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 34141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T14:01:54+00:00 2026-05-10T14:01:54+00:00

In python, you can have a function return multiple values. Here’s a contrived example:

  • 0

In python, you can have a function return multiple values. Here’s a contrived example:

def divide(x, y):     quotient = x/y     remainder = x % y     return quotient, remainder    (q, r) = divide(22, 7) 

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’).

When should you draw the line and define a different method?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T14:01:55+00:00Added an answer on May 10, 2026 at 2:01 pm

    Absolutely (for the example you provided).

    Tuples are first class citizens in Python

    There is a builtin function divmod() that does exactly that.

    q, r = divmod(x, y) # ((x - x%y)/y, x%y) Invariant: div*y + mod == x 

    There are other examples: zip, enumerate, dict.items.

    for i, e in enumerate([1, 3, 3]):     print 'index=%d, element=%s' % (i, e)  # reverse keys and values in a dictionary d = dict((v, k) for k, v in adict.items()) # or  d = dict(zip(adict.values(), adict.keys())) 

    BTW, parentheses are not necessary most of the time. Citation from Python Library Reference:

    Tuples may be constructed in a number of ways:

    • Using a pair of parentheses to denote the empty tuple: ()
    • Using a trailing comma for a singleton tuple: a, or (a,)
    • Separating items with commas: a, b, c or (a, b, c)
    • Using the tuple() built-in: tuple() or tuple(iterable)

    Functions should serve single purpose

    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).

    Sometimes it is sufficient to return (x, y) instead of Point(x, y).

    Named tuples

    With the introduction of named tuples in Python 2.6 it is preferable in many cases to return named tuples instead of plain tuples.

    >>> import collections >>> Point = collections.namedtuple('Point', 'x y') >>> x, y = Point(0, 1) >>> p = Point(x, y) >>> x, y, p (0, 1, Point(x=0, y=1)) >>> p.x, p.y, p[0], p[1] (0, 1, 0, 1) >>> for i in p: ...   print(i) ... 0 1 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 124k
  • Answers 124k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Are you running on OS 3.0? I saw the same… May 12, 2026 at 1:19 am
  • Editorial Team
    Editorial Team added an answer It looks like you need to register Apache::Session::Memcached with Apache::Session::Wrapper,… May 12, 2026 at 1:19 am
  • Editorial Team
    Editorial Team added an answer Use DATENAME or DATEPART: SELECT DATENAME(dw,GETDATE()) -- Friday SELECT DATEPART(dw,GETDATE())… May 12, 2026 at 1:19 am

Related Questions

In python, you can have a function return multiple values. Here's a contrived example:
Questions: What is the best practice for keeping track of a thread's progress without
I am writing a web-framework for Python, of which the goal is to be
What I want to do is build some mini cms which hold pages with
I am trying to numerically integrate an arbitrary (known when I code) function in

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.