[IronPython] PythonType.CreateInstance not calling __init__

Curt Hagenlocher curt at hagenlocher.org
Sat Feb 9 22:13:19 CET 2008


A little more poking suggests that CreateInstance._ctor is really the
equivalent of __new__ and not __init__.  I changed the code to say

    PythonTypeOps.TypeCaller ctor = new PythonTypeOps.TypeCaller(theClass);
    object foo = ctor.Call(context);
and all is now well with the world.
On Feb 9, 2008 9:17 AM, Curt Hagenlocher <curt at hagenlocher.org> wrote:

> The following code creates a Python class "foo" with a single method
> __init__.  It then gets the class object.
>
>     string code = "class foo(object):\n\tdef __init__(self):\n\t\tprint
> 'hello world'\n";
>     SourceUnit scriptSource =
> PythonEngine.CurrentEngine.CreateScriptSourceFromString(code,
> SourceCodeKind.File);
>     IScriptScope scope = PythonEngine.CurrentEngine.CreateScope();
>     PythonEngine.CurrentEngine.Execute(scope, scriptSource);
>     object obj2;
>     scope.TryGetVariable("foo", out obj2);
>     PythonType theClass = obj2 as PythonType;
>
> I then use PythonType.CreateInstance to create the object.
>
>     LanguageContext language = LanguageContext.FromEngine(
> PythonEngine.CurrentEngine);
>     Scope scope2 = new Scope(language);
>     CodeContext context = new CodeContext(scope2, language);
>     object foo = theClass.CreateInstance(context, new object[0]);
> At this point "foo" contains an object with the Python type "foo".
> However, the __init__ method was never called.
>
> What am I doing wrong?
>
> --
> Curt Hagenlocher
> curt at hagenlocher.org
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20080209/1178464c/attachment.html>


More information about the Ironpython-users mailing list