简体   繁体   中英

Is *args in Python3 guaranteed to preserve order?

In Python3 i can use * to accept any number of positional arguments.

A sample demonstrating this:

def a(*args):
    for argument in args:
        print(argument)
a(1,2,3,4)

Would thus print:

1
2
3
4

What I'm uncertain is, if the order of positional arguments stored in args is actually guaranteed to be preserved?

Can I trust that if I call a(1,2,3,4) then args is always (1,2,3,4) or is this just a side effect of an implementation detail?


While trying to look into this, I saw that order in **kwargs is preserved since Python 3.6 and this is specified in PEP-468 how ever I didn't find any mention of *args in this regard.

definitely, it preserves Order because *args take/consider the argument as a tuple data type.

in Python tuple have its Order, always.

Only dictionary is the one data type which will not follow the order in python

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM