[IronPython] Dict methods

JoeSox joesox at gmail.com
Fri Apr 7 03:20:32 CEST 2006


On 4/6/06, Dino Viehland <dinov at exchange.microsoft.com> wrote:
> 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);
>

I was wondering if Ops could help me out, I didn't have enough time to
test it though before I posted my question.


> 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(...)

For what I am doing, it looks like this may be the easiest solution.
I need to make sometime and test this in my project.


> 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.
>

Now that is interesting.  At first, I was playing around with
System.Object and then decided to use int.  It just seems that runtime
might be slower if I used objects and I would possible need GetTypes()
at some point.  I don't know at this point, just thinking out loud
here. But maybe your custom MyDict example may be best.  I need some
time to think this thru.


> Just giving you some options :)

Greatly appreciated!
--
Joseph



More information about the Ironpython-users mailing list