How does * work in Python

Posted by Deqing on Stack Overflow See other posts from Stack Overflow or by Deqing
Published on 2014-08-23T04:17:51Z Indexed on 2014/08/23 4:19 UTC
Read the original article Hit count: 120

Filed under:
|
|

Just switched from C++ to Python, and found that sometimes it is a little hard to understand ideas behind Python.

I guess, a variable is a reference to the real object. For example, a=(1,2,5) meaning a -> (1,2,5), so if b=a, then b and a are 2 references pointing to the same (1,2,5). It is a little like pointers in C/C++.

If I have:

def foo(a,b,c):
  print a,b,c

a=(1,3,5)
foo(*a)

What does * mean here?

Looks like it expands tuple a to a[0], a[1] and a[2]. But why print(*a) is not working while print(a[0],a[1],a[2]) works fine?

© Stack Overflow or respective owner

Related posts about python

Related posts about function