Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #98807 > unrolled thread

Persist objects in a LIST

Started byJohn Zhao <johnzzhao@gmail.com>
First post2015-11-14 06:26 -0800
Last post2015-11-14 17:33 +0100
Articles 4 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  Persist objects in a  LIST John Zhao <johnzzhao@gmail.com> - 2015-11-14 06:26 -0800
    Re: Persist objects in a  LIST John Zhao <johnzzhao@gmail.com> - 2015-11-14 07:02 -0800
      Re: Persist objects in a  LIST Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-11-14 10:39 -0500
      Re: Persist objects in a LIST Vincent Vande Vyvre <vincent.vande.vyvre@telenet.be> - 2015-11-14 17:33 +0100

#98807 — Persist objects in a LIST

FromJohn Zhao <johnzzhao@gmail.com>
Date2015-11-14 06:26 -0800
SubjectPersist objects in a LIST
Message-ID<0cb1bcda-54d5-4cc7-bd23-f86f7f10ee28@googlegroups.com>
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
[{}]

[toc] | [next] | [standalone]


#98809

FromJohn Zhao <johnzzhao@gmail.com>
Date2015-11-14 07:02 -0800
Message-ID<15f9fdeb-f4b4-4404-bccb-2e67a26035ed@googlegroups.com>
In reply to#98807
I found a solution.  replace  bDict.clear() with  bDict = {} 

thanks,

John

[toc] | [prev] | [next] | [standalone]


#98811

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2015-11-14 10:39 -0500
Message-ID<mailman.327.1447515615.16136.python-list@python.org>
In reply to#98809
On Sat, 14 Nov 2015 07:02:41 -0800 (PST), John Zhao <johnzzhao@gmail.com>
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.
-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
    wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

[toc] | [prev] | [next] | [standalone]


#98815 — Re: Persist objects in a LIST

FromVincent Vande Vyvre <vincent.vande.vyvre@telenet.be>
Date2015-11-14 17:33 +0100
SubjectRe: Persist objects in a LIST
Message-ID<mailman.331.1447519455.16136.python-list@python.org>
In reply to#98809
Le 14/11/2015 16:39, Dennis Lee Bieber a écrit :
> On Sat, 14 Nov 2015 07:02:41 -0800 (PST), John Zhao <johnzzhao@gmail.com>
> 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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web