[IronPython] Dict methods

Dino Viehland dinov at exchange.microsoft.com
Fri Apr 7 01:07:50 CEST 2006


If I understand correctly you've got a Dict w/ integer keys and a list of values..

You'll need to cast the result to a List and then do the append:

((List)fw_edges[arg1_uid]).append(fw_edge);

But that will throw if you don't have a real List (no duck typing allowed).  If you wanted to make that work (anything w/ an append method can be called) you could do something like:

Ops.Call(Ops.GetAttr(Ops.GetIndex(fw_edges, arg1_uid), SymbolId.Append), fw_edge);

Not sure if we have SymbolId.Append baked into the system though...  (instead you could do SymbolTable.SymbolToId("append")).


The more C# way to do this would be to use generics, such that you do:

myDict = new Dictionary<int, List>();

then you can do:

myDict[arg1_uid].append(...)

Note you could expose this dictionary out to Python code and it could use it, but it wouldn't be as ideal of an experience as giving the Python developer the real dict.  If you wanted to start getting really crazy you could do:

[PythonType(typeof(Dict))]
class MyDict : Dictionary<int, List>{
}

And then your dictionary would just about appear to be a normal dictionary ,but the user could only ever index off of ints and store lists.

Just giving you some options :)


Do you want to help develop Dynamic languages on CLR? (http://members.microsoft.com/careers/search/details.aspx?JobID=6D4754DE-11F0-45DF-8B78-DC1B43134038)

-----Original Message-----
From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of JoeSox
Sent: Thursday, April 06, 2006 3:39 PM
To: Discussion of IronPython
Subject: [IronPython] Dict methods

Is there any easy method to use to add a value to an
IronPython.Runtime.Dict as seen in the Python code below?

fw_edges[arg1_uid].append(fw_edge)


where IronPython code I am using:
Dict fw_edges = new Dict();   //eg.= {20: [22], 12: [14], 5: [7], 29: [31]}
int arg1_uid;
int fw_edge;

I may be over looking something but I couldn't find a Dict method that
could do the .append to the values already established.
Thanks
--
Joseph
_______________________________________________
users mailing list
users at lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com



More information about the Ironpython-users mailing list