Follow up article after my post, here with more python (3) oddities! Sometimes we find what might seem like a bug in Python but these bugs reveal themselves to be misunderstood features. Following is a collection of some oddities I have found from the recycle bin of the world, known as Internet.

  _   _   _   _   _   _     _   _   _   _   _   _  
 / \ / \ / \ / \ / \ / \   / \ / \ / \ / \ / \ / \ 
( p | y | t | h | o | n ) ( o | d | d | i | t | y )
 \_/ \_/ \_/ \_/ \_/ \_/   \_/ \_/ \_/ \_/ \_/ \_/ 

Python Locals() Oddity

>>> def f():
...   locals()['test_var'] = 10
...   assert locals()['test_var'] == 10
...   print(test_var)
...
>>> f()
Traceback (most recent call last):  
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 4, in f
NameError: name 'test_var' is not defined  

Python sys.getsizeof() Oddity

>>> from sys import getsizeof
>>> s = set(range(100))
>>> getsizeof(s)
8416  
>>> getsizeof(dict.fromkeys(s, None))
4704  

Python Try-Except Oddity

>>> e = 4
>>> try: int('hello')
... except ValueError as e: pass
...
>>> e
Traceback (most recent call last):  
  File "<stdin>", line 1, in <module>
NameError: name 'e' is not defined  

Python2 Boolean Oddity

None=True -> Syntax Error  
0=True -> Syntax Error  
'Foo'=True -> Syntax Error  
False=True -> No Problem  

Python Array Oddity

>>> arr = np.array([1, 8, 2, 3, 9, 5, 4, 7, 6])
>>> arr[::-1].sort()
>>> arr
array([9, 8, 7, 6, 5, 4, 3, 2, 1])  
>>> arr = [1, 8, 2, 3, 9, 5, 4, 7, 6]
>>> arr[::-1].sort()
>>> arr
[1, 8, 2, 3, 9, 5, 4, 7, 6]

Python Similar Arrays

>>> numbers = [10, 15, 20]
>>> numbers2 = numbers
>>> numbers2.append(25)
>>> numbers2
[10, 15, 20, 25]
>>> numbers
[10, 15, 20, 25]
>>> id(numbers)
139670455619848  
>>> id(numbers2)
139670455619848  
>>> numbers is numbers2
True  

Python Recursive Append Oddity

>>> l = []
>>> l.append(l)
>>> l
[[...]]
>>> [[...]]
[[Ellipsis]]

Python Errors? Really? Oddity

>>> temp_list = ([5, 76],)
>>> temp_list[0] += [2, 4]
Traceback (most recent call last):  
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment  
>>> temp_list
([2, 4, 5, 76],)