Live data from Hacker News

Python Oddities

twitter.com

1–10 of 38 posts

Re: Python Oddities

#2
I’ve got one! First, some context:

This is normal assignment unpacking:

    >>> a, b = 1, 2
    >>> [a, b]
    [1, 2]
(Mismatch gives errors)

    >>> a, b = [1]
    ValueError
    >>> a, b = [1, 2, 3]
    ValueError
Extra parentheses works too:

    >>> (a, b) = [2, 3]
    >>> [a, b]
    [2, 3]
Unpacking a single value:

    >>> (a,) = [4]
    >>> a
    4
Works without parentheses too:

    >>> a, = [5]
    >>> a
    5
No values does not work:

    >>> , = []
    SyntaxError
Neither does empty parentheses:

    >>> () = []
    SyntaxError
Now, the oddity:

    >>> [] = []
    >>> 
This raises no error! Neither does this:

    >>> [] = ()
    >>>

Re: Python Oddities

#3
Some of these are pretty simple and not odd, e.g. immutability of tuples, iterator behaviour or modifying collections during iteration.

—————————————————————————————————————

Somewhat related video: https://www.youtube.com/watch?v=qCGofLIzX6g

Re: Python Oddities

#4
post #2

I’ve got one! First, some context: This is normal assignment unpacking: >>> a, b = 1, 2 >>> [a, b] [1, 2] (Mismatch gives errors) >>> a, b = [1] ValueError >>> a, b = [1, 2, 3] ValueError Extra parentheses works too: >>> (a, b) = [2, 3] >>> [a, b] [2, 3] Unpacking a single value: >>> (a,) = [4] >>> a 4 Works without parentheses too: >>> a, = [5] >>> a 5 No values does not work: >>> , = [] SyntaxError Neither does emp…

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)".

Re: Python Oddities

#6
post #2

I’ve got one! First, some context: This is normal assignment unpacking: >>> a, b = 1, 2 >>> [a, b] [1, 2] (Mismatch gives errors) >>> a, b = [1] ValueError >>> a, b = [1, 2, 3] ValueError Extra parentheses works too: >>> (a, b) = [2, 3] >>> [a, b] [2, 3] Unpacking a single value: >>> (a,) = [4] >>> a 4 Works without parentheses too: >>> a, = [5] >>> a 5 No values does not work: >>> , = [] SyntaxError Neither does emp…

Hmm, why is that an oddity? Unpacking a list of N items into N variables works fine, where N >= 0.

Re: Python Oddities

#7
post #5

Best use of Twitter Moments I've ever seen.

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.

Re: Python Oddities

#8
post #2

I’ve got one! First, some context: This is normal assignment unpacking: >>> a, b = 1, 2 >>> [a, b] [1, 2] (Mismatch gives errors) >>> a, b = [1] ValueError >>> a, b = [1, 2, 3] ValueError Extra parentheses works too: >>> (a, b) = [2, 3] >>> [a, b] [2, 3] Unpacking a single value: >>> (a,) = [4] >>> a 4 Works without parentheses too: >>> a, = [5] >>> a 5 No values does not work: >>> , = [] SyntaxError Neither does emp…

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:

    >>> @dis.dis
    ... def foo():
    ...     (a, b) = bar()
    ... 
      3           0 LOAD_GLOBAL              0 (bar)
                  3 CALL_FUNCTION            0 (0 positional, 0 keyword pair)
                  6 UNPACK_SEQUENCE          2
                  9 STORE_FAST               0 (a)
                 12 STORE_FAST               1 (b)
                 15 LOAD_CONST               0 (None)
                 18 RETURN_VALUE
    >>> @dis.dis
    ... def foo():
    ...     [a, b] = bar()
    ... 
      3           0 LOAD_GLOBAL              0 (bar)
                  3 CALL_FUNCTION            0 (0 positional, 0 keyword pair)
                  6 UNPACK_SEQUENCE          2
                  9 STORE_FAST               0 (a)
                 12 STORE_FAST               1 (b)
                 15 LOAD_CONST               0 (None)
                 18 RETURN_VALUE
    >>> @dis.dis
    ... def foo():
    ...     [] = bar()
    ... 
      3           0 LOAD_GLOBAL              0 (bar)
                  3 CALL_FUNCTION            0 (0 positional, 0 keyword pair)
                  6 UNPACK_SEQUENCE          0
                  9 LOAD_CONST               0 (None)
                 12 RETURN_VALUE

Re: Python Oddities

#9
post #6
post #2

I’ve got one! First, some context: This is normal assignment unpacking: >>> a, b = 1, 2 >>> [a, b] [1, 2] (Mismatch gives errors) >>> a, b = [1] ValueError >>> a, b = [1, 2, 3] ValueError Extra parentheses works too: >>> (a, b) = [2, 3] >>> [a, b] [2, 3] Unpacking a single value: >>> (a,) = [4] >>> a 4 Works without parentheses too: >>> a, = [5] >>> a 5 No values does not work: >>> , = [] SyntaxError Neither does emp…

Hmm, why is that an oddity? Unpacking a list of N items into N variables works fine, where N >= 0.

It works when you use a "list literal" as target, but with a "tuple literal" it only works for N > 0.

Re: Python Oddities

#10
Good thing CPython is the spec for the language.

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.

Post reply on HN