#577 Python and C++: classes with same name in different modules

python (259)

When several modules define classes with the same name,
only the module imported first works properly.

For example if I have two modules (a and b) each
defining a class named MyClass, the following code will

import a
import b

ma = a.MyClass()

But the following will raise an exception:

mb = b.MyClass()

It seems that python can't make up its mind whether mb
is of type b.MyClass or a.MyClass.

The problem has appeared with SWIG 1.3.22 and is still
present in 1.3.25.

The attached zip contains the following files:
- a.i and b.i: SWIG sources for both modules;
- build.bat: a windows batch file to build the modules
with mingw-gcc;
- go.py: a sample script showing the problem;
- output.txt: a copy of the output I get from go.py;
- build/*: binaries built with SWIG 1.3.25, mingw-gcc
3.4.2 and Python 2.3.


  • Jérôme M. Berger

    Logged In: YES

    ZIP was too big, here it is as a tar.bz2

    BTW, I just tested on linux Mandrake 10.1, SWIG 1.3.24,
    Python 2.3 and gcc 3.4.3 and the result is the same.

  • Jérôme M. Berger

    Logged In: YES

    Sorry, there's a typo in go.py, line number 15 should read:

    print mb.getData() # <= This raises a TypeError exception

  • Marcelo Matus

    Marcelo Matus - 2005-06-16
    • status: open --> open-later
  • Marcelo Matus

    Marcelo Matus - 2005-06-16

    Logged In: YES

    Yes, by now the interface works like in C++, with the One
    Declaration Rule.


    Encapsulate your modules in namespaces, ie, use

    namespace module_a {

    #include "a.h"




    namespace module_a {

    %include "a.h"


  • Olly Betts

    Olly Betts - 2010-05-26
    • labels: --> python
  • szager

    szager - 2011-04-12

    There is no problem here. There is a typo in the test script. This:

    print mb.GetData() # <= This raises an exception

    ... should be:

    print mb.getData() # <= This raises an exception

    (capitalization of getData is different).

    When I run it, the reported types look fine to me:

    MyClass::MyClass (void) in module a
    <multi_name_conflict_a.MyClass; proxy of <Swig Object of type 'MyClass *' at 0x1696db0> >
    MyClass::getData() in module a => 0
    MyClass::setData (1) in module a
    MyClass::getData() in module a => 1
    MyClass::MyClass (void) in module b
    <multi_name_conflict_b.MyClass; proxy of <Swig Object of type 'MyClass *' at 0x1696b40> >
    MyClass::getData() in module b => 0

    (Note that I renamed the modules to multi_name_conflict_[ab], but the code is the same.)

    Marking invalid.

  • szager

    szager - 2011-04-12
    • assigned_to: nobody --> szager
    • status: open-later --> closed-invalid

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.

No, thanks