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
user3856093
131 gold badge1 silver badge3 bronze badges
-
2You really need to go through the tutorial.Burhan Khalid– Burhan Khalid2014年07月19日 14:40:29 +00:00Commented Jul 19, 2014 at 14:40
-
1I 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.user3856093– user38560932014年07月20日 17:16:18 +00:00Commented 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 thisjgh fun-run– jgh fun-run2022年02月10日 03:15:44 +00:00Commented Feb 10, 2022 at 3:15
-
And I just noticed that this is 8 years old, sorry for necroingjgh fun-run– jgh fun-run2022年02月10日 03:17:02 +00:00Commented Feb 10, 2022 at 3:17
1 Answer 1
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
Cory Kramer
119k19 gold badges177 silver badges233 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
chepner
+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.lang-py