[Python] What does *tuple and **dict mean in Python? [duplicate]

Discussão em 'Python' iniciado por Stack, Agosto 18, 2025.

  1. Stack

    Stack Membro Participativo

    As mentioned in PythonCookbook, * can be added before a tuple. What does * mean here?


    Chapter 1.18. Mapping Names to Sequence Elements:

    from collections import namedtuple
    Stock = namedtuple('Stock', ['name', 'shares', 'price'])
    s = Stock(*rec)
    # here rec is an ordinary tuple, for example: rec = ('ACME', 100, 123.45)​

    In the same section, **dict presents:


    from collections import namedtuple
    Stock = namedtuple('Stock', ['name', 'shares', 'price', 'date', 'time'])
    # Create a prototype instance
    stock_prototype = Stock('', 0, 0.0, None, None)
    # Function to convert a dictionary to a Stock
    def dict_to_stock(s):
    return stock_prototype._replace(**s)​

    What is **'s function here?

    Continue reading...

Compartilhe esta Página