[IronPython] How to reference List objects?

Justin Regele jregele23 at gmail.com
Tue Jun 9 03:39:32 CEST 2009


Can C# objects implement iterable protocols? Maybe that is why he was
getting the 'object is unscriptable' error.

But good to know that it works.

On Mon, Jun 8, 2009 at 6:06 PM, Larry Danberger
<larry.danberger at nubium.com>wrote:

> Hi All,
> I switched from a list to a dictionary of dictionaries, and it works as I
> needed it.  Don't know if that's a performance hit or not, but hey it
> works.
>
> ..and yes I was retyping and had two typos (good eye!), should have been
> TypeError: 'List[book]' object is unsubscriptable   not  'List[books]'
> object is unsubscriptable, and my code declared the book class methods as
> public.
>
> Thank you all for your help.  I'm sure this won't be my last question!
>
> -Larry
>
> -----Original Message-----
> From: users-bounces at lists.ironpython.com
> [mailto:users-bounces at lists.ironpython.com] On Behalf Of Curt Hagenlocher
> Sent: Monday, June 08, 2009 3:02 PM
> To: Discussion of IronPython
> Subject: Re: [IronPython] How to reference List objects?
>
> It indicates a nested class:
>
> class PythonEngine {
>    class book {
>    }
> }
>
> You can definitely interact with BCL generic List classes in this fashion:
>
> >>> from System.Collections.Generic import List a = List[str]()
> >>> a.Add('abc')
> >>> a.Add('def')
> >>> a
> List[str](['abc', 'def'])
> >>> a[0]
> 'abc'
> >>> ^Z
>
> There are a number of subtle problems with the output text in the original
> report; was this typed in or copy-and-pasted?
>
>
> On Mon, Jun 8, 2009 at 1:55 PM, Justin Regele <jregele23 at gmail.com> wrote:
>
>
>        Ah, I see. Would a python list then need to be called the way he is
> doing it, though. Is this a silverlight issue? I am wondering if he can
> just
> make a python list out of his Book C# class rather than exporting them as a
> list. I am sure there would be some performance issues, so maybe that is
> the
> reason for this way of doing it.
>
>        Wondering about this error:
>
>        System.Collections.Generic.
>        List'1[testapp.PythonEngine+book]
>
>
>        What does the '+' indicate?
>
>
>
>        On Mon, Jun 8, 2009 at 1:32 PM, Michael Foord
> <fuzzyman at voidspace.org.uk> wrote:
>
>
>                Justin Regele wrote:
>
>
>                        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.
>
>
>
>                He is creating the list from C# and making it available to
> Python code. As far as I can tell he is doing it right, although maybe the
> fact that his C# book class is private is interfering with it?
>
>                Michael
>
>
>
>
>
>                        On Mon, Jun 8, 2009 at 11:27 AM, Larry Danberger
> <larry.danberger at nubium.com <mailto: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
> <mailto:Users at lists.ironpython.com>
>
>
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
>
>
>
> ------------------------------------------------------------------------
>
>
>                        _______________________________________________
>                        Users mailing list
>                        Users at lists.ironpython.com
>
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
>
>
>
>
>                --
>                http://www.ironpythoninaction.com/
>                http://www.voidspace.org.uk/blog
>
>
>
>                _______________________________________________
>                Users mailing list
>                Users at lists.ironpython.com
>
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
>
>
>
>        _______________________________________________
>        Users mailing list
>        Users at lists.ironpython.com
>        http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
>
>
>
>
> _______________________________________________
> 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/3f09e090/attachment.html>


More information about the Ironpython-users mailing list