About Tuple len=1 in C API

Hi,

Is it possible to differentiate when the user uses some Tuplet len=1 or just one number, for example:


import mymod

def mysum(x, y):
    mymod.func((x + y))
    mymod.func(x + y)

With this, for example:

static PyObject *Py4pdMod_PdOut(PyObject *self, PyObject *args){
    (void)self;
    printf("%s", PyUnicode_AsUTF8(PyObject_Str(args)));
   ...

I have (7,) in both lines.

Thank you!

That’s not a tuple, that’s simply a paranthesized operation. To create a single-element tuple, you must use a comma: (x + y,)

1 Like

Sorry, my bad. But my question persists.

Is it possible to differentiate when the user uses some Tuplet len=1 or just one number, for example:

import mymod

def mysum(x, y):
    mymod.func((x + y),)
    mymod.func(x + y)

Thanks!

That’s not a 1-tuple. The outer parentheses in mymod.func((x + y),) belong to the parameter list, and the final comma is just a trailing comma, which is ignored.

It’s a subtle point that a trailing comma is ignored, except when it’s being used to indicate a 1-tuple:

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

and also:

>>> 1,
(1,)
>>> 1, 2
(1, 2)
>>> 1, 2,
(1, 2)
2 Likes

Thank you!!!