Is there something in Python that is similar to "want" in Perl

Viewed 93

In Perl there is a way to find out what context a function is called in - whether it's a list or scalar context - or even finer granularity. See wantarray and the want module.

Is there something similar in Python?

1 Answers

No, Python doesn't have the scalar/array distinction that Perl does. Values are simply bound to names with no regard for the type of the value.

The closest analogy I can think of would be something like

x += y

This is desugared as x.__iadd__(y), so the method __iadd__ could examine the type of its argument y if it wanted to. For example, even though list.__iadd__ doesn't do this, it could theoretically have been defined to allow things like

x = []
x += 9  # x == [9]; append a non-iterable argument
x += [11, 12]  # x == [9, 11, 12]; extend with an iterable argument
Related