Live data from Hacker News

Python Oddities

twitter.com

11–20 of 38 posts

Re: Python Oddities

#12
Am I the only one who hated the mobile pop-out slideshow Twitter uses for this?

Also, the one about appending to arrays didn't seem too crazy.

Re: Python Oddities

#14
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)

Re: Python Oddities

#16

Here's one from https://stackoverflow.com/questions/15171695/whats-with-the-... : In [1]: a = 1 In [2]: b = 1 In [3]: a is b Out[3]: True In [4]: a = 257 In [5]: b = 257 In [6]: a is b Out[6]: False

Yes, because the smaller ints are 'pre-constructed' in Python (hence all 1's are the same "int(1)" object)

Re: Python Oddities

#17
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 appears to no longer be the case in Python 3:

    $ python2
    Python 2.7.13 (default, Jul 21 2017, 03:24:34) 
    >>> () = []
      File "", line 1
    SyntaxError: can't assign to ()
    >>>

    $ python3
    Python 3.6.2 (default, Jul 20 2017, 03:52:27) 
    >>> () = []
    >>>

Re: Python Oddities

#18
post #11

Aha, three ways to concatenate arrays! I see Python conforms to the TIMTOWTDI principle. :)

There should be one _obvious_ way to do it. The obvious way is using +, and is sufficient for 90% of programs.

Re: Python Oddities

#19
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

Re: Python Oddities

#20
post #14

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 tu…

[deleted]
Post reply on HN