0

I'm trying to instantiate a class written in C++ from python. For some reason, I'm getting a syntax error when invoking the "print" method, which takes no argument and should just print an int:

IronPython 2.7.5b2 (2.7.5.0) on .NET 4.0.30319.18444 (32-bit)
Type "help", "copyright", "credits" or "license" for more information.
>>> import clr
>>> clr.AddReferenceToFileAndPath('c:\\users\\pletzer\\documents\\visual studio
>>> \\Projects\\AlexTest\\Debug\\AlexTest.dll')
>>> import at
>>> a = at.AlexTest(2)
>>> a.print()
  File "<stdin>", line 1
    a.print()
  ^

SyntaxError: syntax error

Thanks in advance for any suggestion. The C++ class is

// AlexTest.h

#include <iostream>
#pragma once

using namespace System;

namespace at {

    public ref class AlexTest
    {
    public:
        AlexTest(int i) {
            mi = i;
        }
        void print() {
            std::cout << "mi = i\n";
        }
    private:
        int mi;
    };
}

1 Answer 1

1

Changing the name of the method from "print" to "display" fixes the issue.

Also, can use (raw string r'...')

clr.AddReferenceToFileAndPath(r'c:\users\pletzer\documents\visual studio\Projects\AlexTest\Debug\AlexTest.dll')

to avoid having to type double back slashes

Sign up to request clarification or add additional context in comments.

1 Comment

The reason is that print is a keyword in (Iron)Python 2 and thus can't be used as a member name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.