0

Is it possible to return two separate object in one 'return' statement? I have tried below but I get 'tuple' object.

recipient, user = authenticate(mobile=mobile, email=email)

Function:

def authenticate(self, mobile=None, email=None):
     user = Recipient.objects.recipient_friend_match(mobile, email)
     return user[0], user
7
  • 4
    Unpack the tuple: x, y = function(). Commented Feb 26, 2014 at 7:33
  • is that not what I'm doing here: recipient, user = Commented Feb 26, 2014 at 7:34
  • 2
    You can only ever return one thing, but that one thing can be a tuple, which you can unpack at the call site (i.e. what you're doing with recipient, user ). Commented Feb 26, 2014 at 7:34
  • so can I do this: return user[0], user ? Commented Feb 26, 2014 at 7:34
  • 5
    Your code should work, but why return user[0] separately when it's already included in user? Commented Feb 26, 2014 at 7:36

2 Answers 2

2

return tuple is right,you can get them like this

def fun():
    a = 4
    b = [1,2,3,4]
    return a,b # equals to return (a,b)

a,b=fun()

just print a and b to see the results

Sign up to request clarification or add additional context in comments.

1 Comment

if more,like this, a,b,c,d,e = [1,2,3,4,5] # or (1,2,3,4,5)
0

or you can use dict like this

def fun():
    a = 1
    b = 2
    return dict(x=a, y=b)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.