[DB-SIG] Dictionaries Pass By Reference?

Duane and Karen duaneh@connexus.net.au
Sat, 2 Nov 2002 17:01:57 +1100


This is a multi-part message in MIME format.

------=_NextPart_000_0001_01C28291.B10B45D0
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
X-MIME-Autoconverted: from 8bit to quoted-printable by power.connexus.net.au id gA261gJM098250

Sorry, I actually replied to the questioner's address, so I'm hoping I se=
nt
this to the right place. Following is a transcript of my earlier reply
(Sorry Tom.)
"
I actually bypassed that and passed a dictionary like so..
=A0
### code start ###
def get_new_dict_values(dict):
=A0=A0=A0 # change values
=A0=A0=A0 dict['key1'] =3D 'new value 1'
=A0=A0=A0 dict['key3'] =3D 'new key added'
=A0=A0=A0 return dict
=A0
def main():
=A0=A0=A0 # set up initial dicitonary
=A0=A0=A0 dict =3D {}
=A0=A0=A0 dict['key1'] =3D 'initial value 1'
=A0=A0=A0 dict['key2'] =3D 'initial value 2'
=A0=A0=A0 print dict
=A0=A0=A0 dict =3D get_new_dict_values(dict)
=A0=A0=A0 print dict
=A0=A0=A0 return
=A0
main()
### code end ###
=A0
The reason I would do this is because Python is a pure object oriented
language and as such it will reset the original dictionary object to the =
new
one, which is just a manipulated result of the one you passed. I cannot s=
ee
any ByReference (like VB) unless you make the dictionary global like the
following code, in which case you would not have to pass it... (Though th=
at
is just my opinion and I have yet to find the facts, but this seems logic=
al
for the language. :) )
=A0
### code start ###
=A0
def get_new_dict_values():
=A0=A0=A0 # change values
=A0=A0=A0 global dict
=A0=A0=A0 dict['key1'] =3D 'new value 1'
=A0=A0=A0 dict['key3'] =3D 'new key added'
=A0=A0=A0 return
=A0
def main():
=A0=A0=A0 # set up initial dicitonary
=A0=A0=A0 global dict
=A0=A0=A0 dict['key1'] =3D 'initial value 1'
=A0=A0=A0 dict['key2'] =3D 'initial value 2'
=A0=A0=A0 print dict
=A0=A0=A0 dict =3D get_new_dict_values()
=A0=A0=A0 print dict
=A0=A0=A0 return
=A0
### Set global dictionary
=A0
dict =3D {}
=A0
main()
=A0
### code end ###
=A0
Hope this helps,
=A0
Duane.
"
Duane Hennessy
Glen Iris, Victoria, Australia
=A0

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.401 / Virus Database: 226 - Release Date: 10/9/2002


------=_NextPart_000_0001_01C28291.B10B45D0
Content-Type: text/html;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META HTTP-EQUIV=3D"Content-Type" CONTENT=3D"text/html; =
charset=3Diso-8859-1">


<META content=3D"MSHTML 6.00.2712.300" name=3DGENERATOR></HEAD>
<BODY>
<DIV><FONT face=3DArial size=3D2><SPAN class=3D232345705-02112002>Sorry, =
I actually=20
replied to the questioner's address, so I'm hoping I sent this to the =
right=20
place. Following is a transcript of my earlier reply (Sorry=20
Tom.)</SPAN></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><SPAN=20
class=3D232345705-02112002>"</SPAN></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><SPAN class=3D232345705-02112002>I =
actually bypassed=20
that and passed a dictionary like so..</SPAN></FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2><SPAN class=3D232345705-02112002>### =
code start=20
###<BR>def get_new_dict_values(dict):<BR>&nbsp;&nbsp;&nbsp; # change=20
values<BR>&nbsp;&nbsp;&nbsp; dict['key1'] =3D 'new value =
1'<BR>&nbsp;&nbsp;&nbsp;=20
dict['key3'] =3D 'new key added'<BR>&nbsp;&nbsp;&nbsp; return=20
dict</SPAN></FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2><SPAN class=3D232345705-02112002>def=20
main():<BR>&nbsp;&nbsp;&nbsp; # set up initial =
dicitonary<BR>&nbsp;&nbsp;&nbsp;=20
dict =3D {}<BR>&nbsp;&nbsp;&nbsp; dict['key1'] =3D 'initial value=20
1'<BR>&nbsp;&nbsp;&nbsp; dict['key2'] =3D 'initial value =
2'<BR>&nbsp;&nbsp;&nbsp;=20
print dict<BR>&nbsp;&nbsp;&nbsp; dict =3D=20
get_new_dict_values(dict)<BR>&nbsp;&nbsp;&nbsp; print =
dict<BR>&nbsp;&nbsp;&nbsp;=20
return</SPAN></FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2><SPAN =
class=3D232345705-02112002>main()<BR>### code=20
end ###</SPAN></FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2><SPAN class=3D232345705-02112002>The =
reason I would=20
do this is because Python is a pure object oriented language and as such =
it will=20
reset the original dictionary object to the new one, which is just a =
manipulated=20
result of the one you passed. I cannot see any ByReference (like VB) =
unless you=20
make the dictionary global like the following code, in which case you =
would not=20
have to pass it... (Though that is just my opinion and I have yet to =
find the=20
facts, but this seems logical for the language. :) )</SPAN></FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2><SPAN class=3D232345705-02112002>### =
code start=20
###</SPAN></FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2><SPAN class=3D232345705-02112002>def=20
get_new_dict_values():<BR>&nbsp;&nbsp;&nbsp; # change=20
values<BR>&nbsp;&nbsp;&nbsp; global dict<BR>&nbsp;&nbsp;&nbsp; =
dict['key1'] =3D=20
'new value 1'<BR>&nbsp;&nbsp;&nbsp; dict['key3'] =3D 'new key=20
added'<BR>&nbsp;&nbsp;&nbsp; return</SPAN></FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2><SPAN class=3D232345705-02112002>def=20
main():<BR>&nbsp;&nbsp;&nbsp; # set up initial =
dicitonary<BR>&nbsp;&nbsp;&nbsp;=20
global dict<BR>&nbsp;&nbsp;&nbsp; dict['key1'] =3D 'initial value=20
1'<BR>&nbsp;&nbsp;&nbsp; dict['key2'] =3D 'initial value =
2'<BR>&nbsp;&nbsp;&nbsp;=20
print dict<BR>&nbsp;&nbsp;&nbsp; dict =3D=20
get_new_dict_values()<BR>&nbsp;&nbsp;&nbsp; print =
dict<BR>&nbsp;&nbsp;&nbsp;=20
return</SPAN></FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2><SPAN class=3D232345705-02112002>### =
Set global=20
dictionary</SPAN></FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2><SPAN class=3D232345705-02112002>dict =
=3D=20
{}</SPAN></FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2><SPAN=20
class=3D232345705-02112002>main()</SPAN></FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2><SPAN class=3D232345705-02112002>### =
code end=20
###</SPAN></FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2><SPAN class=3D232345705-02112002>Hope =
this=20
helps,</SPAN></FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2><SPAN=20
class=3D232345705-02112002>Duane.</SPAN></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><SPAN=20
class=3D232345705-02112002>"</SPAN></FONT></DIV>
<DIV><FONT face=3DArial size=3D2>Duane Hennessy</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>Glen Iris, Victoria, =
Australia</FONT></DIV>
<DIV>&nbsp;</DIV></BODY></HTML>
<BR>

<P><FONT SIZE=3D2>---<BR>
Outgoing mail is certified Virus Free.<BR>
Checked by AVG anti-virus system (http://www.grisoft.com).<BR>
Version: 6.0.401 / Virus Database: 226 - Release Date: 10/9/2002<BR>
</FONT> </P>

------=_NextPart_000_0001_01C28291.B10B45D0--