This isn't doing what you think though. There are container objects underneath.
For instance "(a, b)" is "a tuple object containing two things, the values named "a" and "b" so you can still refer to them separately. "[a, b]" is a new list with those same elements, not an alias for "(a, b)".
The oddity is that unpacking an empty iterable works when the target is a "list", but not when it's a "tuple". In fact it so doesn't work it actually fails during parsing.
And there are no collections underneath the LHS, it compiles to an UNPACK_SEQUENCE opcode in both cases:
It’s a good point that Python is smart enough not to build a mutable list on the right-hand side when not needed (e.g. for "x = [1, 2]" it will and for "y, z = [1, 2]" it will not). A right-hand tuple however is still a separate type, and if you disassemble the original cases you can see that "[] = []" is more wasteful than "[] = ()".
First use of Twitter Moments I've ever seen. What is this? A collection of tweets? I'm confused because they all seem to be by the author, did he tweet them himself and collected them?
The page's UI looks weird, I'm not sure if Twitter had a redesign or if I'm blocking some of their CSS.
I think they're pretty much a collection of tweets. This was the first time I used moments and I'm not sure where I saw them used before I made this.
They're mostly by me because I made a goal for myself to tweet out one of these every week for a year. Others have shared their own and I've tweeted my own since then, but Twitter's hashtag search is pretty bad so I made a moment.
I think it works quite well, I just hadn't seen the feature before. I disagree with you a bit, though, in that many seemed pretty regular, and weren't really oddities, in my opinion.
That said, many of these "oddities" are disingenuous to the point of lying, many of the error messages are omitted. Or the functionality converts to list before doing an operation. Not that it is good library or language design, but this is a great way to inflate bug counts.
Some of them seem like litmus test for a Python programmer interview, others feel like WTFs Python LoL! It is hard to disambiguate them. Standing on their own in that twitter feed, with little context, there is next to no enlightenment.
Lots of the oddities could be explained by
In [1]: list({'a':1, 'b':2, 'c':3})
Out[1]: ['a', 'b', 'c']
Lots of Python makes sense. About 3% does not. It is good to know the 3% so it doesn't bite one in the ass. Maybe switch to image posts? 1 megapixel limit.
Vast majority of these are not actually oddities. A few stuck out at me, though:
- since True/False are aliased to 1/0, you cannot have both 1 and True as dictionary keys. This could plausibly happen in real code.
- there's no distinction between args/kwargs, at least when it comes to a function's __defaults__ attr. An arg can be turned into a kwarg by messing with the __defaults__ attr.
- you cannot have a nested tuple/dict/list with more than sys.MAX_RECURSION levels, because each list access apparently counts as a new call frame? (This could plausibly happen when generating a dict from json or html or xml)
At the python level, all named args[0] are kwargs. The reverse is not true in Python 3, although it is in Python 2. At the C level, arguments can be exclusively one or the other even in Python 2.
> at least when it comes to a function's __defaults__ attr. An arg can be turned into a kwarg by messing with the __defaults__ attr.
No, an arg can be turned optional by manipulating __defaults__, that is a different (though not quite orthogonal) axis, especially in Python 3 (which supports python-level required keyword arguments).
> - since True/False are aliased to 1/0, you cannot have both 1 and True as dictionary keys.
They are not aliased (`True is 1` will return False) but they are equal and hash identically.
cpython oddity: The following program (call it "20.py") takes a minute to run, but the reported time for the "x=..." line is a few microseconds.
import time
if 1:
t1 = time.time()
x = ((((((((0,)*20,)*20,)*20,)*20,)*20,)*20,)*20,)*20
t2 = time.time()
print(t2-t1)
% /usr/bin/time python 20.py
3.09944152832e-06
70.91 real 70.91 user 0.24 sys
Automatic Reference Counting, i.e. garbage collection. That's why the timer wouldn't attribute the CPU time to the line you mention; the GC cleans the structure up when the "x" goes out of scope, so after the timing has ended.
Within class bodies, symbols prefixed with two underscores get "mangled" (prefixed with the class name).
This is intended to avoid risks of collisions for classes built specifically for inheritance so you'd usually use it with attributes e.g. `self.__name = name` (that way if a subclass defines its own "name" things will work as expected for both). I was unaware this also worked for straight local variables.
I was unaware it happened for all symbols, but I guess it makes sense as the Python compiler does very little analysis.
> But tuples can be mutated in-place: >>> b = (1,) >>> b += (2,) >>> b (1, 2)
That's not an in-place mutation any more than
>>> a = 1
>>> a += 2
is.
The oddity is that most people assume `a += b` is the same as `a = a + b`, but Python allows overloading both separately and list.__iadd__ is an alias for list.extend, hence being able to += any iterable onto a list whereas `list + non_list` generates a type error.
The original intent is an optimisation (to avoid copying the subject list) but it's a broken one as it behaves very inconsistently with regular concatenation/addition: it mutates the subject in-place and allows a different (wider) set of parameters.
37 comments
[ 3.5 ms ] story [ 97.9 ms ] threadThis is normal assignment unpacking:
(Mismatch gives errors) Extra parentheses works too: Unpacking a single value: Works without parentheses too: No values does not work: Neither does empty parentheses: Now, the oddity: This raises no error! Neither does this:For instance "(a, b)" is "a tuple object containing two things, the values named "a" and "b" so you can still refer to them separately. "[a, b]" is a new list with those same elements, not an alias for "(a, b)".
And there are no collections underneath the LHS, it compiles to an UNPACK_SEQUENCE opcode in both cases:
It was fixed in Python 3.6 specifically, the error still occurs under 3.5.
—————————————————————————————————————
Somewhat related video: https://www.youtube.com/watch?v=qCGofLIzX6g
The page's UI looks weird, I'm not sure if Twitter had a redesign or if I'm blocking some of their CSS.
They're mostly by me because I made a goal for myself to tweet out one of these every week for a year. Others have shared their own and I've tweeted my own since then, but Twitter's hashtag search is pretty bad so I made a moment.
That said, many of these "oddities" are disingenuous to the point of lying, many of the error messages are omitted. Or the functionality converts to list before doing an operation. Not that it is good library or language design, but this is a great way to inflate bug counts.
I left out error messages (and occasionally other things) because Twitter.
I don't think any of the ones I put in this moment should be considered bugs. Many of these are core language features.
Lots of the oddities could be explained by
When people would expect Lots of Python makes sense. About 3% does not. It is good to know the 3% so it doesn't bite one in the ass. Maybe switch to image posts? 1 megapixel limit.Also, the one about appending to arrays didn't seem too crazy.
- since True/False are aliased to 1/0, you cannot have both 1 and True as dictionary keys. This could plausibly happen in real code.
- there's no distinction between args/kwargs, at least when it comes to a function's __defaults__ attr. An arg can be turned into a kwarg by messing with the __defaults__ attr.
- you cannot have a nested tuple/dict/list with more than sys.MAX_RECURSION levels, because each list access apparently counts as a new call frame? (This could plausibly happen when generating a dict from json or html or xml)
At the python level, all named args[0] are kwargs. The reverse is not true in Python 3, although it is in Python 2. At the C level, arguments can be exclusively one or the other even in Python 2.
> at least when it comes to a function's __defaults__ attr. An arg can be turned into a kwarg by messing with the __defaults__ attr.
No, an arg can be turned optional by manipulating __defaults__, that is a different (though not quite orthogonal) axis, especially in Python 3 (which supports python-level required keyword arguments).
> - since True/False are aliased to 1/0, you cannot have both 1 and True as dictionary keys.
They are not aliased (`True is 1` will return False) but they are equal and hash identically.
[0] *args notwithstanding
Add:
to the bottom of the code and it still takes a minute to run, even though there's no final garbage collection.Also, that code creates only a few hundred objects, and the 'x' data structure is acyclic.
This is intended to avoid risks of collisions for classes built specifically for inheritance so you'd usually use it with attributes e.g. `self.__name = name` (that way if a subclass defines its own "name" things will work as expected for both). I was unaware this also worked for straight local variables.
I was unaware it happened for all symbols, but I guess it makes sense as the Python compiler does very little analysis.
But tuples can be mutated in-place: >>> b = (1,) >>> b += (2,) >>> b (1, 2)
The odd thing is that lists can be in-place added (+='d) to any iterable, while tuples can only be in-place added to tuples.
I think it works this way so that += on lists works consistently with the extend method on lists, which also accepts any iterable.
That's not an in-place mutation any more than
is.The oddity is that most people assume `a += b` is the same as `a = a + b`, but Python allows overloading both separately and list.__iadd__ is an alias for list.extend, hence being able to += any iterable onto a list whereas `list + non_list` generates a type error.
The original intent is an optimisation (to avoid copying the subject list) but it's a broken one as it behaves very inconsistently with regular concatenation/addition: it mutates the subject in-place and allows a different (wider) set of parameters.