python - Different ways of deleting lists -


i want understand why:

  • a = [];
  • del a; and
  • del a[:];

behave differently.

i ran test each illustrate differences witnessed:

>>> # test 1: reset = [] ...  >>> = [1,2,3] >>> b = >>> = [] >>> [] >>> b [1, 2, 3] >>>  >>> # test 2: reset del ...  >>> = [1,2,3] >>> b = >>> del >>> traceback (most recent call last):   file "<stdin>", line 1, in <module> nameerror: name 'a' not defined >>> b [1, 2, 3] >>>  >>> # test 3: reset del a[:] ...  >>> = [1,2,3] >>> b = >>> del a[:] >>> [] >>> b [] 

i did find different ways of clearing lists, didn't find explanation differences in behaviour. can clarify this?

test 1

>>> = [1,2,3] # set point list [1, 2, 3] >>> b = # set b pointing @ >>> = [] # set point empty list  # step 1: --> [1 2 3]  # step 2: --> [1 2 3] <-- b  # step 3: --> [     ] [1 2 3] <-- b  # @ point points new empty list # whereas b points original list of 

test 2

>>> = [1,2,3] # set point list [1, 2, 3] >>> b = # set b pointing @ >>> del # delete reference list  # step 1: --> [1 2 3]  # step 2: --> [1 2 3] <-- b  # step 3:       [1 2 3] <-- b  # no longer exists because reference # destroyed b not affected because # b still points original list 

test 3

>>> = [1,2,3] # set point list [1, 2, 3] >>> b = # set b pointing @ >>> del a[:] # delete contents of original  # step 1: --> [1 2 3]  # step 2: --> [1 2 3] <-- b  # step 2: --> [     ] <-- b  # both , b empty because pointing  # same list elements removed   

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -