Python Oddities
11–20 of 38 posts
Re: Python Oddities
#12Also, the one about appending to arrays didn't seem too crazy.
Re: Python Oddities
#13Re: Python Oddities
#14- 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
#15 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]: FalseRe: Python Oddities
#16Here'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
Re: Python Oddities
#17I’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…
$ 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
#18Aha, three ways to concatenate arrays! I see Python conforms to the TIMTOWTDI principle. :)
Re: Python Oddities
#19 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 sysRe: Python Oddities
#20Vast 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…