0

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.

Allu2
  • 111
  • 2

1 Answers1

1

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

abhi krishnan
  • 821
  • 6
  • 20
  • 2
    And even dicts will with Python 3.6/3.7+ – Bernhard Jul 20 '18 at 06:01
  • While the order of `**kwargs` is documented and specified in PEP-468, `*args` is only mentioned to be wrapped into a tuple. While I know tuple will keep its order once made I'm concerned whether its specified that when said tuple is formed, it is done with intention to retain order of the undefined positional arguments. I'm not too familiar with how python maps the given arguments for the function into that tuple, thus the question is more specifically if the resulting tuple is specified to be formed in order of inserted parameters. Or just happens to be due to way it is implemented in CPython – Allu2 Jul 20 '18 at 08:03