4

I've pass by some variables while documenting myself for animation with matplotlib which are written like this ln, = plt.plot([], [], 'ro', animated=True) or return ln, , and I don't understand what the comma stands for...

It's probably related to tuple but I don't understand how, as the comma is supposed to be after the value and not the variable like i = 1,

Thanks in advance for any clarification,


EDIT: From the response suggested in comment by @Goralight.

In Python, it's the comma that makes something a tuple:

>>> 1
1
>>> 1,
(1,)

The parenthesis are optional in most locations.

VictorGalisson
  • 635
  • 9
  • 27
  • 3
    https://stackoverflow.com/a/16037517/4278756 – Goralight Dec 01 '17 at 09:43
  • 2
    It creates a tuple. `i = 1,` is equivalent to `i = (1,)`. All you need to do to see this is type it into the interpreter, then see what `i` is, right? Similarly, `return ln,` is equivalent to `return (ln,)`, and `ln, = ...` is equivalent to `(ln,) = ...` – Tom Karzes Dec 01 '17 at 09:47
  • @Goralight thanks, I couldn't find it by myself – VictorGalisson Dec 01 '17 at 10:11
  • 1
    @TomKarzes `ln, = ` is a special case. No tuple is created on for the left-hand side of the assignment; it's just syntax for a sequence of `STORE_NAME` instructions. – chepner Oct 25 '19 at 14:15
  • @chepner In programming language theory, the construct is known as a "destructuring assignment", or "unpacking". The syntax on the left matches the the structure of the data on the right, and the individual elements are assigned accordingly. Of course it doesn't actually create the tuple. The point is that it uses the same syntax. – Tom Karzes Oct 26 '19 at 00:15

1 Answers1

1

You can find it on 5. Data Structures - Python 3.6.3 documentation

A tuple consists of a number of values separated by commas
[...]
Tuples are immutable, and usually contain a heterogeneous sequence of elements that are accessed via unpacking or indexing

KL_
  • 1,503
  • 1
  • 8
  • 13