AttributeError:__ getattr__

时间:2015-02-06 17:36:27

标签: python swig

我是Python和SWIG的新手,所以这可能是一个常见的错误。我有以下简单的C ++类:

Header File:

class myMath
{
public:
    myMath(void);
    ~myMath(void);

    int add(int x, int y);
    int minusOne(int x);
    void sub(int *x, int *y, int *result);
    int divide(int n, int d, int *r);
    double avg_array(double *array, int len);
    int sum_array(int *array, int len);

};

C++ File:
/* File : example.cpp */

#include "Example.h"

myMath::myMath()
{
}

myMath::~myMath()
{
}

int myMath::add(int x, int y) 
{
    return x + y;
}

int myMath::minusOne(int x)
{
    return x - 1;
}

void myMath::sub(int *x, int *y, int *result) 
{
    *result = *x - *y;
}

int myMath::divide(int n, int d, int *r) 
{
    int q;
    q = n/d;
    *r = n - q*d;
    return q;
}

double myMath::avg_array(double *array, int len)
{
    double sum = 0.0;
    double avg;
    for (int i = 0; i < len; i++)
    {
        sum += array[i];
    }

    avg = sum / (double) len;

    return avg;
}

int myMath::sum_array(int *array, int len)
{
    int sum = 0;

    for (int i = 0; i < len; i++)
    {
        sum += array[i];
    }

    return sum;
}

Interface File:

/* File : example.i */
%module example

%{
    #include "Example.h"
%}

%include "Example.h"

这是Python代码:

# file: swigExample.py

import sys

# Include the local Python modules
sys.path.append('C:/Temp/PTest_3')

import example

a = 37
b = 42
c = 0

print "     a =",a
print "     b =",b
print "     c =",c

ex = example.myMath()

d = ex.myMath.minusOne( 10 )
print "     d =",d

这是我运行Python代码时遇到的错误:

Traceback (most recent call last):
  File "C:\Temp\PTest_3\swigExample.py", line 20
    d = ex.myMath.minusOne( 10 )
  File "C:\Temp\PTest_3\example.py", line 94, in <lambda>
    __getattr__ = lambda self, name: _swig_getattr(self, myMath, name)
  File "C:\Temp\PTest_3\example.py", line 71, in _swig_getattr
    return _swig_getattr_nondynamic(self, class_type, name, 0)
  File "C:\Temp\PTest_3\example.py", line 66, in _swig_getattr_nondynamic
    return object.__getattr__(self, name)
 AttributeError: type object 'object' has no attribute '__getattr__'

我确定这一定是非常基本的,但我在网站上找不到类似的问题。提前谢谢。

0 个答案:

没有答案