proper name for python * operator?
Asked Answered
S

9

276

What is the correct name for operator *, as in function(*args)? unpack, unzip, something else?

Stonge answered 23/2, 2010 at 22:48 Comment(8)
Javascript has the equivalent spread ... operator.Goldeneye
Just for reference, in the C++, it is called Dereference Operator (ref: cplusplus.com/doc/tutorial/pointers). Additionally, in Python, all variable to function (arguments) is pass by assignment. (ref: docs.python.org/3/faq/…)Eskew
@CloudCho The one in C++ stands for something entirely different.Cibis
@Sнаđошƒаӽ Would you describe the difference between Dereference Operator (C++) and Unpacking Operator (Python)? I thought both of them related to memory location rather than value in memory. Thanks.Eskew
@CloudCho Take a look at the official documentation.Cibis
@Sнаđошƒаӽ, Thanks for the link. I see the first example with * is not related to memory, but the second example with ** and Dictionary data structure looks different. When I print(*Dictionary), it shows only Key in Dictionary. Would you give additional information? Thanks.Eskew
@CloudCho, the C++ analogue of Python's * is also ..., used in template parameter packs.Yellowknife
I call it the "gather/scatter" operator. Mirrored semantics in definitions and in expressions. In definitions it's "gather", in expressions - "scatter". Single star works for anonymous objects being gathered/scattered into/from an iterable, ** - for named ones and dictionaries.Donegal
A
241

In Ruby and Perl 6 this has been called "splat", and I think most people from those communities will figure out what you mean if you call it that.

The Python tutorial uses the phrase "unpacking argument lists", which is long and descriptive.

It is also referred to as iterable unpacking, or in the case of **, dictionary unpacking.

Autostrada answered 23/2, 2010 at 22:53 Comment(7)
Well in python it's also used to pack argument lists, so in that context should it be called unsplat? :)Idalla
@THC4k I propose splatsplat.Meed
Unfortunately INTERCAL does not have * as an operator so we're lacking for an official source...Tolle
In JavaScript (es2015) it is known as the spread operator.Leprose
Python docs.Dialyse
I know it as splat from JuliaMacedonian
I searched for "python splat" to find this page because of the usage in other languages.Airline
A
131

I call it "positional expansion", as opposed to ** which I call "keyword expansion".

Atropine answered 24/2, 2010 at 2:40 Comment(0)
F
55

The Python Tutorial simply calls it 'the *-operator'. It performs unpacking of arbitrary argument lists.

Finable answered 23/2, 2010 at 22:57 Comment(5)
This is the most accurate answer and it's a shame it wasn't accepted!Levanter
@alfasin: The expression 'the *-operator' is ambiguous since * – depending on the context – can perform either argument expansion or multiplication which are two different operations.Skeptic
@Skeptic and it's really not difficult to understand which one it is from the context it's in.Levanter
Seems like the OP was asking how to pronounciate (which may or may not be a word itself - that's another rabbit hole) *. By saying it's the *-operator is simply throwing the question back as an answer. OP probably wanted to know how to say it out loud (or in his head) when he encounters it. This is all new to me, and I'll be going with "splat!".Diversiform
If we go for simple let's call it star-operator. But ** could be dasterisk. Hm..Overmeasure
I
17

I say "star-args" and Python people seem to know what i mean.

** is trickier - I think just "qargs" since it is usually used as **kw or **kwargs

Idalla answered 23/2, 2010 at 23:10 Comment(1)
I also just say kwargs, although that doesn't really refer to the operator itself I suppose.Churn
C
17

One can also call * a gather parameter (when used in function arguments definition) or a scatter operator (when used at function invocation).

As seen here: Think Python/Tuples/Variable-length argument tuples.

Carmella answered 15/7, 2011 at 12:53 Comment(2)
but you can't yell gather or scatter like you can with SPLAT :DAssure
@SamanthaBranham I like how gather and scatter can be easily and distinctly visualized. Of course splat visualizes great, too 😂💥Carmella
C
10

I believe it's most commonly called the "splat operator." Unpacking arguments is what it does.

Colicroot answered 23/2, 2010 at 22:55 Comment(0)
G
3

For a colloquial name there is "splatting".

For arguments (list type) you use single * and for keyword arguments (dictionary type) you use double **.

Both * and ** is sometimes referred to as "splatting".

See for reference of this name being used: https://mcmap.net/q/66944/-is-there-an-object-spread-syntax-in-python-2-7x-like-in-javascript

Gyroplane answered 13/10, 2020 at 11:40 Comment(0)
S
3

The technical term for this is a Variadic function. So in a sense, that's the correct term without regard to programming language.

That said, in different languages the term does have legitimate names. As others have mentioned, it is called "splat" in ruby, julia, and several other languages and is noted by that name in official documentation. In javascript it is called the "spread" syntax. It has many other names in many other languages, as mentioned in other answers. Whatever you call it, it's quite useful!

Snyder answered 2/6, 2021 at 23:4 Comment(0)
T
-1

I call *args "star args" or "varargs" and **kwargs "keyword args".

Tolle answered 9/5, 2011 at 21:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.