Why does the **kwargs mapping compare equal with a differently ordered OrderedDict?

前端 未结 4 375
挽巷
挽巷 2021-01-17 12:00

According to PEP 468:

Starting in version 3.6 Python will preserve the order of keyword arguments as passed to a function. To accomplish this the coll

4条回答
  •  攒了一身酷
    2021-01-17 12:19

    Just to add, if you do want to make this check (without relying on an implementation detail (which even then, won't be in python 3.7)), just do

    from collections import OrderedDict
    >>> data = OrderedDict(zip('xy', 'xy'))
    >>> def foo(**kwargs):
    ...     return OrderedDict(kwargs) == data
    

    since this is guaranteed to be True.

提交回复
热议问题