Liqun
Liqun

Reputation: 4171

What does the python tuple multiplied by some constant mean?

I read some python codes like follows:

color = (1.0,)*4

I couldn't figure out what does it mean? (1.0,) means some tuple, but what does it mean by multiplying 4 here?

Upvotes: 0

Views: 115

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1123930

You create a new tuple with 4 times the same referenced value.

>>> (1.0,) * 4
(1.0, 1.0, 1.0, 1.0)

See the Sequence types reference

s * n, n * s
n shallow copies of s concatenated

Note that it's the exact same value that is reused; you see this when you use a mutable value:

>>> lst = []
>>> tup = (lst,) * 4
>>> tup[0] is lst
True
>>> all(i is lst for i in tup)
True

Upvotes: 3

Related Questions