When defining a functiondef test (* args)And when receiving multiple arguments, I think that it will be received as a tuple.
Here, if you specify a list that stores some numbers in the argument of the test method,* argsTo([5,6,4])It looks like this, but here on the method side(5,6,4)If I want to, what should you do?
Since tuples are immutable, take the value for minutes from the list and try again.argsI think you can do something like that.
I would appreciate it if you could tell me what kind of method can be considered in such a case.

  • Answer # 1


    On the calling side*I think it's normal to develop iterables.

    def test (* args):
        print (args)
    lst = [5, 6, 4]
    test (* lst)


    (5, 6, 4)

    def test (* args)When preparing the argument definition, the intention is

    test (4, 5, 6)

    Is expected to be called.

    On the other hand[4, 5, 6]If you want to pass it to the original, it is a line that the caller will match it with * expansion.

    On the other handFrom the caller's point of view, if "always pass a value based on one list", then * expansion is useless, in which case "the person who defined the function made a mistake". Become.

    Such a function

    def test (lst):
        "" "
        : param lst: Iterable containing int

    It was more natural to define something like this.

  • Answer # 2

    What about tuple ([5,6,4])?

  • Answer # 3

    In general, list-to-tuple conversion can be done with the tuple () function.

    l = [5,6,4]
    t = tuple (l) # t = (5,6,4)

Related articles