简体   繁体   English

是否可以保证Python3中的* args保留顺序?

[英]Is *args in Python3 guaranteed to preserve order?

In Python3 i can use * to accept any number of positional arguments. 在Python3中,我可以使用*接受任意数量的位置参数。

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? 我不确定的是,是否确实保证保留了args存储的位置参数的顺序?

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? 我可以相信,如果我调用a(1,2,3,4)那么args 总是 (1,2,3,4)还是仅仅是实现细节的副作用?


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. 在尝试研究这一点时,我看到自Python 3.6起, **kwargs中的顺序得以保留,而在PEP-468中对此进行了指定,但是我在这方面却没有提到*args

definitely, it preserves Order because *args take/consider the argument as a tuple data type. 无疑,它保留了Order,因为* args将参数视为元组数据类型。

in Python tuple have its Order, always. 在Python中,元组总是有其顺序。

Only dictionary is the one data type which will not follow the order in python 只有字典是一种不会遵循python中的顺序的数据类型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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