Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

การทำงานของ * Operator บน Tuple ใน Python

Star * operator unpacks sequence/collection เป็น positional arguments หากมี tuple ต้องการส่ง items เป็น arguments ตามตำแหน่ง แทนที่จะ index ทีละตัว ใช้ * operator

ตัวอย่าง

def multiply(a, b):
    return a * b

values = (1, 2)
print(multiply(*values))

Unpack tuple ให้ execute จริงเป็น:

print(multiply(1, 2))

ผลลัพธ์

2