Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Quick python question I hope
New Posts  All Forums:Forum Nav:

Quick python question I hope

post #1 of 2
Thread Starter 
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.
Croissant
(15 items)
 
  
CPUMotherboardGraphicsRAM
2600k Asus Sabertooth Z77 GTX 680 2x4 gigs G-SKill 
Hard DriveOptical DriveCoolingOS
Crucial C300 2x Asus DVD-RW Silver Arrow Win 7 
MonitorPowerCaseAudio
Dual ASUS 24" Rosewill 750 Capstone Corsair Carbide Air 540 The Basic sound blaster Z-Series 
  hide details  
Reply
Croissant
(15 items)
 
  
CPUMotherboardGraphicsRAM
2600k Asus Sabertooth Z77 GTX 680 2x4 gigs G-SKill 
Hard DriveOptical DriveCoolingOS
Crucial C300 2x Asus DVD-RW Silver Arrow Win 7 
MonitorPowerCaseAudio
Dual ASUS 24" Rosewill 750 Capstone Corsair Carbide Air 540 The Basic sound blaster Z-Series 
  hide details  
Reply
post #2 of 2
Quote:
Originally Posted by Thereoncewasamaninparis View Post

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.

>>> 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)

Edited by hajile - 8/2/13 at 2:44pm
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Quick python question I hope