[C++-sig] When You Create an Python Object Using Boost.Python, How Do You Find Out Its Name?

Lawrence Spector Lawrence.Spector at CanfieldSci.com
Mon Jul 9 22:51:12 CEST 2007


In your example, it should have a name of foo, which you can call methods on.

The problem I'm having is I want to be able to create the object using the boost::python wrappers around the Python-C API, but access it from a script.

So, this works:

foo = MyClass()
foo.callMethod()

However, when I do this in C++:

object foo(MyClass());
object result = exec("foo.callMethod()", main_namespace, main_namespace);

This fails on the second line, with:
        NameError: name 'foo' is not defined.

I guess, the first question would be is: "object foo(MyClass())" the equivalent to foo = MyClass()?  If so, how do I make foo the name that is in the dict.  If not, how do I write the equivalent of foo = MyClass() using Boost.Python?

Thanks,

Lawrence

-----Original Message-----
From: c++-sig-bounces at python.org [mailto:c++-sig-bounces at python.org] On Behalf Of Nat Goodspeed
Sent: Monday, July 09, 2007 4:37 PM
To: Development of Python/C++ integration
Subject: Re: [C++-sig] When You Create an Python Object Using Boost.Python, How Do You Find Out Its Name?

________________________________________
From: c++-sig-bounces at python.org [mailto:c++-sig-bounces at python.org] On Behalf Of Lawrence Spector
Sent: Monday, July 09, 2007 3:05 PM
To: c++-sig at python.org
Subject: [C++-sig] When You Create an Python Object Using Boost.Python, How Do You Find Out Its Name?

When I do something like this:

boost::python::object pyClass(MyInstance);

What name does it give it?  It doesn't appear it to give it pyClass as the name, but it has to pick something to identify itself for the Python-C API.  So, what's the name and how do I find out what it is?

[Nat] Should it have a name? If I write in a Python script:

class MyInstance(object):
    pass

foo = MyInstance()

the object instantiated by the MyInstance() call does not have an embedded name. The script's locals dict acquires an entry mapping "foo" to that new instance, but the name "foo" belongs to the dict rather than to the object.

Maybe I don't understand your question.
_______________________________________________
C++-sig mailing list
C++-sig at python.org
http://mail.python.org/mailman/listinfo/c++-sig



More information about the Cplusplus-sig mailing list