[IronPython] How to reference List objects?

Larry Danberger larry.danberger at Nubium.com
Mon Jun 8 11:27:27 PDT 2009


Hi all,
I'm new to Python and IronPython, sorry if this is obvious.  I haven't found
answer searching web or in IronPython In Action book...  

I have embedded IronPython into silverlight app for scripting, which is
working (wow! Btw).  I use scope.SetVariable for dictionaries which works as
expected.

However when passing in a list I am unable to access the objects within a
list.
For the example below when trying 
x = books[0].Name 
I get 
TypeError: 'List[books]' object is unsubscriptable

If I do
x = books
I get back
System.Collections.Generic.List'1[testapp.PythonEngine+book] 

If I do
Len(books) 
I get back 4.

How do I access them individually (by name etc.)?

My code looks something like this:

Class book
{
   int ID { get; set; }
   String Name { get; set; }
   String Author { get; set; }
   String Description { get; set; }
}

...
List<book> _books = new List<book>();

_books.Add(new book { ID=1, Name="book1", Author="author1",
Description="Description1"});
_books.Add(new book { ID=2, Name="book2", Author="author2",
Description="Description2"});
_books.Add(new book { ID=3, Name="book3", Author="author3",
Description="Description3"});
_books.Add(new book { ID=4, Name="book4", Author="author4",
Description="Description4"});

...
_scope.SetVariable("books", _books);


Any help appreciated, thanks!
-Larry



More information about the Users mailing list