Overclock.net banner

1 - 2 of 2 Posts
Joined
·
264 Posts
Discussion Starter · #1 ·
Code:

Code:
#!/usr/bin/python

colors = ["red","blue","green","yellow","brown","black"]

months = (
    "Jan","Feb","Mar","Apr","May","Jun",
    "Jul","Aug","Sep","Oct","Nov","Dec",
)

for c in enumerate(colors):
    print "{0} {1}".format(*c)

print

for num,month in enumerate( months, 1 ):
    print "{0} {1}".format( num, month )
The (*c) syntax is what is throwing me off, it's looks very much like a ptr, but obviously isn't.

I assume it's use is for the {0} and the {1} since only one variable is provided, I just can't find anything on google, any idea what the name is of that * operator? Even when I find a list of python images, I don't get * for multiplication, *= assignment and ** for exponents.
 

·
Premium Member
Joined
·
1,601 Posts
Quote:
Originally Posted by Thereoncewasamaninparis View Post

Code:

Code:
#!/usr/bin/python

colors = ["red","blue","green","yellow","brown","black"]

months = (
    "Jan","Feb","Mar","Apr","May","Jun",
    "Jul","Aug","Sep","Oct","Nov","Dec",
)

for c in enumerate(colors):
    print "{0} {1}".format(*c)

print

for num,month in enumerate( months, 1 ):
    print "{0} {1}".format( num, month )
The (*c) syntax is what is throwing me off, it's looks very much like a ptr, but obviously isn't.

I assume it's use is for the {0} and the {1} since only one variable is provided, I just can't find anything on google, any idea what the name is of that * operator? Even when I find a list of python images, I don't get * for multiplication, *= assignment and ** for exponents.
Without me retyping, here's an answer on stackoverflow.

http://stackoverflow.com/questions/5239856/foggy-on-asterisk-in-python

Basically, it flattens the list the splat is before (or in this case, a variable that dereferences to a list).

Here's an rip of one of the examples at said source as I'm too lazy to write out my own right now.

Code:
>>> def foo(a, b=None, c=None):
...   print a, b, c
... 
>>> foo([1, 2, 3])
[1, 2, 3] None None
>>> foo(*[1, 2, 3])
1 2 3
>>> def bar(*a):
...   print a
... 
>>> bar([1, 2, 3])
([1, 2, 3],)
>>> bar(*[1, 2, 3])
(1, 2, 3)
 
1 - 2 of 2 Posts
Top