Persist objects in a LIST

John Zhao johnzzhao at gmail.com
Sat Nov 14 09:26:29 EST 2015


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
[{}]



More information about the Python-list mailing list