[IronPython] Calling functions in IronPython that don't really exist

Dino Viehland dinov at microsoft.com
Thu Dec 4 23:12:38 CET 2008


You can just import static functions instead.  Something like:

public static class ScriptHelpers {
        public static object AddNumbers(params object[] args) {
                return 42;
        }
}

Add the reference and then:

from ScriptHelpers import *

If you really need to construct an instance for some reason you could do it inside of AddNumbers and do the .GetReturn there as well.

> -----Original Message-----
> From: users-bounces at lists.ironpython.com [mailto:users-
> bounces at lists.ironpython.com] On Behalf Of Jeff Slutter
> Sent: Thursday, December 04, 2008 1:59 PM
> To: Discussion of IronPython
> Subject: Re: [IronPython] Calling functions in IronPython that don't
> really exist
>
> Actually this doesn't exactly work. If I want the command to be able to
> return a value, I can't go with the constructor route:
>
> res = AddNumbers( 3, 5 );
>
> you want res equal to "8", but really it is an instance of my
> AddNumbers
> class. I don't want the script writers to be able to save this class
> instance, and it would be 'ugly' if they had to call ".GetReturn()" to
> get the return value of the command.
>
> And I don't want the call to "Do" to be explicit.
>
> I sound really picky...
>
>
> Dino Viehland wrote:
> > Do you mean you'd call it like "AddNumbers().Do(3, 4)"?
> >
> > This is really easy.  Make AddNumbers public, then do:
> >
> > import clr
> > clr.AddReference('MyAssembly')
> > import AddNumbers
> > AddNumbers().Do(3, 4)
> >
> > If you really want to do AddNumbers(3, 4) then you'd just write it
> as:
> >
> > public class AddNumbers
> > {
> >     public AddNumbers(params object[] args) {
> >          Do(args);
> >     }
> >     public string Do( params object[] args )
> >     {
> >         ..check args..
> >         ..add the two arguments..
> >         ..return the result as a string..
> >     }
> > }
> >
> > And do the same:
> >
> > import clr
> > clr.AddReference('MyAssembly')
> > import AddNumbers
> > AddNumbers(3, 4)
> >
> _______________________________________________
> 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