-3

What does

(foo, bar) = foobar()

mean in Python?

The original question doesn't explain () on the left of the =, unless you already know the answer and the title is ambiguous

asked Jul 19, 2014 at 14:35
4
  • 2
    You really need to go through the tutorial. Commented Jul 19, 2014 at 14:40
  • 1
    I have to translate a Python program into Java so I don't know Python, and how do you google or search stackoverflow for "()". But thanks for the help. PS. The duplicate question doesn't explain () on the left of the =, unless you already know the answer. Commented Jul 20, 2014 at 17:16
  • 1
    @user3856093 who assigns someone who doesn't know both languages to translate from one to the other? Also Java is like the worst language to translate into, there's no good reason to do it, especially from Python. Nim or Rust is what I'd recommend since they will have a closer translation than Java,althoughEvnC++ would also be better, really all Java is in this case is an OOP systems programming language but slower (just knowing that you're translating from Python at least, I know of very few jobs Java is better for than Python & C++ &&&). Although I'm guessing you didn't have a choice in this Commented Feb 10, 2022 at 3:15
  • And I just noticed that this is 8 years old, sorry for necroing Commented Feb 10, 2022 at 3:17

1 Answer 1

7

It takes the outputs of the function foobar(), then unpacks them into the variables foo and bar respectively.

>>> def foobar():
 return 1,2
>>> foobar()
(1, 2)
>>> (foo,bar) = foobar()
>>> foo
1
>>> bar
2
answered Jul 19, 2014 at 14:36
Sign up to request clarification or add additional context in comments.

2 Comments

+1, but technically, no tuple is created, at least not by the C implementation of Python 2.7. The tuple returned by foobar is unpacked and the two values are assigned to foo and bar, respectively.
@chepner is right. No objects are ever created on the left of the = sign. Only names are assigned. The parenthesis here simply make the code more readable and are actually not necessary.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.