Hello! My name is Thomas and I'm a nerd. I like tech and gadgets and speculative fiction, and playing around with programming. It's not my day job, but I'm working on making it a side gig :)
Oh, and generally working against the language and its idioms by shoehorning things into place instead of picking an appropriate data structure or language construct from the outset.
Examples:
Wanting dicts to be ordered instead of using OrderedDict (this is changed in Python 3.7+).
Looping through dicts and accessing elements by key instead of using the appropriate iterator.
Using for loops and indexing to access parts of lists instead of using slices.
We created an OrderedDict, but the constructor was using **kwargs which means the arguments we passed to it were stored in a regular dict and got reordered before OrderedDict.__init__ could store them in order.
With Python 3.6+, the **kwargs stores the arguments in the same order they were written, and the OrderedDict construction works as expected:
Technically dicts have been ordered since 3.6 on (C)Python, it's just that in 3.7 they decided that to be call "Python 3.7" any implementation has to have the insertion order.
So, dicts in Python 3.6+ are ordered only in the official version, which means that you might have portability issues if you depend on that behavior, but in 3.7+ you are garanteed they will be ordered, regardless of the implementation.
Hello! My name is Thomas and I'm a nerd. I like tech and gadgets and speculative fiction, and playing around with programming. It's not my day job, but I'm working on making it a side gig :)
Oh, and generally working against the language and its idioms by shoehorning things into place instead of picking an appropriate data structure or language construct from the outset.
Examples:
Wanting dicts to be ordered instead of using OrderedDict (this is changed in Python 3.7+).
Looping through dicts and accessing elements by key instead of using the appropriate iterator.
Using for loops and indexing to access parts of lists instead of using slices.
Things like that.
dict
ordering is important because keyword arguments are stored in a dict. Consider this:We created an
OrderedDict
, but the constructor was using**kwargs
which means the arguments we passed to it were stored in a regulardict
and got reordered beforeOrderedDict.__init__
could store them in order.With Python 3.6+, the
**kwargs
stores the arguments in the same order they were written, and theOrderedDict
construction works as expected:A little detail on ordered dicts:
Technically dicts have been ordered since 3.6 on (C)Python, it's just that in 3.7 they decided that to be call "Python 3.7" any implementation has to have the insertion order.
So, dicts in Python 3.6+ are ordered only in the official version, which means that you might have portability issues if you depend on that behavior, but in 3.7+ you are garanteed they will be ordered, regardless of the implementation.
Difference with Python 2:
Yep, that's what I meant by 3.7 being different on that point.