0

I'm trying to understand a function in python which requires a single parameter. But this parameter is followed by a comma. Ex:

def api_integration(request,):
   ...

Is that an error or does it make syntactical sense?

It_is_Chris
  • 13,504
  • 2
  • 23
  • 41
  • Try to call it (or even just run the file as is) and see what you get. –  Jan 27 '22 at 20:28
  • I have seen and personally used [trailing commas](https://docs.python.org/3/faq/design.html#why-does-python-allow-commas-at-the-end-of-lists-and-tuples) in lists and dictionaries but not in functions. But it wont cause a problem. – It_is_Chris Jan 27 '22 at 20:29
  • In python the comma is used to indicate there are multiple elements, for example for tuples you can find `("one",)` where the comma is required to say it's a tuple containing one element and not a string ; `("one")` would be the same as `"one"`. Consequently, it is, in my understanding of Python, correct in the syntax to have a comma without anything after. I don't know about function parameters, but I in your case guess it would just be a useless comma which would be correct syntactically but not change anything – Kureteiyu Jan 27 '22 at 20:30
  • It's called a "Trailing comma". Maybe this thread will help you out --> https://stackoverflow.com/questions/11597901/why-are-trailing-commas-allowed-in-a-list – Maximilian Freitag Jan 27 '22 at 20:40
  • Have a look [PEP8](https://www.python.org/dev/peps/pep-0008/#when-to-use-trailing-commas) _When trailing commas are redundant, they are often helpful when a version control system is used, when a list of values, arguments or imported items is expected to be extended over time. The pattern is to put each value (etc.) on a line by itself, always adding a trailing comma, and add the close parenthesis/bracket/brace on the next line. However it does not make sense to have a trailing comma on the same line as the closing delimiter (except in the above case of singleton tuples)_ IMHO it's redundant – buran Jan 27 '22 at 20:51

0 Answers0