Skip to content

Instantly share code, notes, and snippets.

@hsnee
Created April 3, 2019 21:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hsnee/81c2b7a0a9fe140f9e4ba3c33f8fba63 to your computer and use it in GitHub Desktop.
Save hsnee/81c2b7a0a9fe140f9e4ba3c33f8fba63 to your computer and use it in GitHub Desktop.
unexpected behavior in python
myhome$ ipython
Python 3.7.2 (default, Feb 12 2019, 08:15:36)
Type 'copyright', 'credits' or 'license' for more information
IPython 6.5.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: # comparisons
In [2]: a = 256
In [3]: b = 256
In [4]: a is b
Out[4]: True
In [5]: a == b
Out[5]: True
In [6]: a = 257
In [7]: b = 257
In [8]: a is b
Out[8]: False
In [9]: a == b
Out[9]: True
In [10]: id(a)
Out[10]: 4399267056
In [11]: id(b)
Out[11]: 4399267664
In [12]: a is 257
Out[12]: False
In [13]: a == 257
Out[13]: True
In [14]: a = 3
In [15]: b = 3
In [16]: a is b
Out[16]: True
In [17]: clear
In [18]: # scope
In [19]: a = 1
In [20]: def f(x):
...: return a
...:
...:
In [21]: f(2)
Out[21]: 1
In [22]: def f(x):
...: a += 1
...: return a
...:
...:
In [23]: f(2)
---------------------------------------------------------------------------
UnboundLocalError Traceback (most recent call last)
<ipython-input-23-c510dc86724b> in <module>()
----> 1 f(2)
<ipython-input-22-c9c3272a51e0> in f(x)
1 def f(x):
----> 2 a += 1
3 return a
UnboundLocalError: local variable 'a' referenced before assignment
In [24]: def f(x):
...: a = 1
...: return
...:
...:
In [25]: a = 2
In [26]: f(3)
In [27]: a
Out[27]: 2
In [28]: def f(x):
...: a = 1
...: print(a)
...: return
...:
...:
In [29]: f(3)
1
In [30]: a
Out[30]: 2
In [31]: clear
In [32]: # comparisons
In [33]: 3 is 1 + 2
Out[33]: True
In [34]: 0.3 is 0.1 + 0.2
Out[34]: False
In [35]: 0.3 == 0.1 + 0.2
Out[35]: False
In [36]: 0 is False
Out[36]: False
In [37]: 0 == False
Out[37]: True
In [38]: 0.1+0.2
Out[38]: 0.30000000000000004
In [39]: round(0.1, 1)
Out[39]: 0.1
In [40]: '' is ''
Out[40]: True
In [41]: 0 is 0
Out[41]: True
In [42]: [] is []
Out[42]: False
In [43]: {} is {}
Out[43]: False
In [44]: clear
In [45]: # comparisons
In [46]: x = (1 << 53) + 1
In [47]: x
Out[47]: 9007199254740993
In [48]: x + 1. < x
Out[48]: True
In [49]: x + 1 < x
Out[49]: False
In [50]: False is False
Out[50]: True
In [51]: True in [True]
Out[51]: True
In [52]: False is False in [True]
Out[52]: False
In [53]: (False is False) in [True]
Out[53]: True
In [54]: 0 is 0 in [1]
Out[54]: False
In [55]: (0 is 0) in [1]
Out[55]: True
In [56]: clear
In [57]: # Immutability
In [58]: a = ([1], )
In [59]: a
Out[59]: ([1],)
In [60]: a[0] += [2]
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-60-94399e3f4fce> in <module>()
----> 1 a[0] += [2]
TypeError: 'tuple' object does not support item assignment
In [61]: a
Out[61]: ([1, 2],)
In [62]: a[0].extend(3)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-62-be2fd989379d> in <module>()
----> 1 a[0].extend(3)
TypeError: 'int' object is not iterable
In [63]: a[0].extend([3])
In [64]: a
Out[64]: ([1, 2, 3],)
In [65]: [1] + 2
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-65-54324b7054ad> in <module>()
----> 1 [1] + 2
TypeError: can only concatenate list (not "int") to list
In [66]: type(a[0])
Out[66]: list
In [67]: type(a)
Out[67]: tuple
In [68]: a[0].append(3)
In [69]: a
Out[69]: ([1, 2, 3, 3],)
In [70]: clear
In [71]: # copying
In [72]: a = [1] * 3
In [73]: a
Out[73]: [1, 1, 1]
In [74]: b = a
In [75]: a[0] = 1000
In [76]: a
Out[76]: [1000, 1, 1]
In [77]: b
Out[77]: [1000, 1, 1]
In [78]: c = a[:]
In [79]: a
Out[79]: [1000, 1, 1]
In [80]: c
Out[80]: [1000, 1, 1]
In [81]: a[0] = 1
In [82]: a
Out[82]: [1, 1, 1]
In [83]: c
Out[83]: [1000, 1, 1]
In [84]: d = a.copy()
In [85]: a = [[1, 2, 3], [4, 5, 6]]
In [86]: b = a[:]
In [87]: a[0][0] = 1000
In [88]: a
Out[88]: [[1000, 2, 3], [4, 5, 6]]
In [89]: b
Out[89]: [[1000, 2, 3], [4, 5, 6]]
In [90]: import copy
In [91]: d = copy.deepcopy(a)
In [92]: a
Out[92]: [[1000, 2, 3], [4, 5, 6]]
In [93]: d
Out[93]: [[1000, 2, 3], [4, 5, 6]]
In [94]: a[0][0] = 1
In [95]: a
Out[95]: [[1, 2, 3], [4, 5, 6]]
In [96]: d
Out[96]: [[1000, 2, 3], [4, 5, 6]]
In [97]: copy.copy
Out[97]: <function copy.copy(x)>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment