[IronPython] PythonType.CreateInstance not calling __init__

Curt Hagenlocher curt at hagenlocher.org
Sat Feb 9 18:17:11 CET 2008


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/e969cc01/attachment.html>


More information about the Ironpython-users mailing list