[IronPython] How to reference List objects?

Justin Regele jregele23 at gmail.com
Mon Jun 8 22:30:11 CEST 2009


Type Errors where the object is unscriptable means that it does not have any
indices intialized.

Use the python interactive interpreter to play around with list methods, and
see how they work correctly. This is one of pythons best weapons. if you
don't know what objects are in a module, or what attributes/methods are in a
class type in dir(<module or classname>) and it will spell it out for you.

you do need to initialize a list before access, however.
so you CANT do this

x = []
x[0] = 'something'

you would have to do this(making a list of 10 None objects)
x = [None] * 10
x[0] = 'something'

OR use the append() method to make the list more like a dynamic array

x = []
x.append('something')

this is the python way of doing it, but i'm not sure why you need a .NET
list array.

On Mon, Jun 8, 2009 at 11:27 AM, Larry Danberger <larry.danberger at nubium.com
> wrote:

> 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
>
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20090608/6db34cb4/attachment.html>


More information about the Ironpython-users mailing list