Re: Persist objects in a LIST

2015-11-14 Thread Vincent Vande Vyvre

Le 14/11/2015 16:39, Dennis Lee Bieber a écrit :

On Sat, 14 Nov 2015 07:02:41 -0800 (PST), John Zhao 
declaimed the following:


I found a solution.  replace  bDict.clear() with  bDict = {}


Which is creating a second, empty, dictionary and binding the name
"bDict" to that new one.

If all you want is to get rid of the name

del bDict

should suffice.

Not exactly.

>>> l = []
>>> d = {'key': 'val'}
>>> l.append(d)
>>> l
[{'key': 'val'}]
>>> del d
>>> l
[{'key': 'val'}]
>>> del l[0]
>>> l
[]
>>>

Vince
--
https://mail.python.org/mailman/listinfo/python-list


Re: Persist objects in a LIST

2015-11-14 Thread John Zhao
I found a solution.  replace  bDict.clear() with  bDict = {} 

thanks,

John
-- 
https://mail.python.org/mailman/listinfo/python-list


Persist objects in a LIST

2015-11-14 Thread John Zhao
I am new to Python, and just learned that Python list is just a container of 
object reference.   

In the example below,  bDict needs to be just a temporary object, constructed 
at run time and then be added to aList.   At the end, aList will contain n 
objects.

Is there a clean way to do that?

Many thanks, 

John

$ ipython
WARNING: IPython History requires SQLite, your history will not be saved
WARNING: Readline services not available or not loaded.
WARNING: The auto-indent feature requires the readline library
Python 2.7.9 (default, Sep  5 2015, 07:20:36)
Type "copyright", "credits" or "license" for more information.

IPython 4.0.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help  -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.
In [1]: aList = []
In [2]: bDict = {}
In [3]: bDict['instance_name']='SERVER_TIER'
In [4]: aList.append(bDict)
In [5]: print aList
[{'instance_name': 'SERVER_TIER'}]
In [6]: bDict.clear()
In [7]: print aList
[{}]
-- 
https://mail.python.org/mailman/listinfo/python-list