3

I am new to python and Django and am trying to determine how the following code works:

return HttpResponseRedirect(reverse('poll_results', args=(p.id,)))

Specifically the 2nd argument of the reverse function. It looks like it is setting the parameter of args equal to a tuple. Why do I need an extra comma?

Chris Muench
  • 17,444
  • 70
  • 209
  • 362

2 Answers2

7

(p.id) is just p.id in parentheses, (p.id,) is a single-element tuple.

Parenthesized forms in docs

Pavel Anossov
  • 60,842
  • 14
  • 151
  • 124
3

The trailing comma is required if a tuple only has one item to differentiate a tuple from stylistic parenthesis.

Similar questions:
Python tuple comma syntax rule
Why does adding a trailing comma after a string make it a tuple

Community
  • 1
  • 1
John M.
  • 2,234
  • 4
  • 24
  • 36