Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7180645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:23:32+00:00 2026-05-28T17:23:32+00:00

I have the following code in C++: class Person { public: enum Gender {Male,

  • 0

I have the following code in C++:

class Person
{
    public:
        enum Gender {Male, Female};

        Gender GetGender() const;
}

I wrapped it in boost::python in this way:

BOOST_PYTHON_MODULE(TestPython)
{
    scope the_scope = class_<Person>("Person")
        .def("GetGender", &Person::GetGender);

    enum_<Person::Gender>("Gender")
        .value(Male, Person::Male)
        .value(Female, Person::Female)
        .export_values();
}

When I try to call person.GetGender() from Python I get the following exception:

Can't pickle : attribute lookup **PyBF.TestPython.Gender**.
It guesses the namespace of the Gender (which is actually **PyBF.TestPython.Person.Gender**) enum return type incorrectly.

How can I tell the GetGender function what type to return explicitly?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T17:23:33+00:00Added an answer on May 28, 2026 at 5:23 pm

    Since we do not have the code that produces the error, I assume it occurs when you try to pickle a Person object.

    Your problem is not specifically related to the usage of boost. It lies in cPickle module. It has problem pickling object with nested classes. See this answer for an explanation. Here’s a simple code example that produces the error:

    import cPickle
    
    class MyOuterClass(object):
        class MyInnerClass(object):
            pass
    
        def __init__(self):
            self.my_inner_class = self.MyInnerClass()
    
    
    def pickle_error():
        print "Pickling ..."
        my_outer_class = MyOuterClass()
        print cPickle.dumps(my_outer_class)
    
    if __name__ == "__main__":
        pickle_error()
    

    Running it produces this output:

    Pickling ...
    Traceback (most recent call last):
      File "pickle.py", line 18, in <module>
        pickle_error()
      File "pickle.py", line 15, in pickle_error
        print cPickle.dumps(my_outer_class)
    cPickle.PicklingError: Can't pickle <class '__main__.MyInnerClass'>: 
    attribute lookup __main__.MyInnerClass failed
    

    As mentioned in the linked answer, when cPickle asks inner class for its name it returns '__main__.MyInnerClass'. However, this name can’t be found in module’s namespace, hence you get the exception.


    Now you experience this because since there isn’t something similar to an enum type in python, boost create an object to represent it. The enum_ construct declares a class in the current scope. By capturing the class scope, you end up creating a nested class within Person and you get the pickle error mentioned above.

    There’s a couple of solutions to your problem.

    The simplest would be to declare the enum outside of Person’s scope. You may not want to expose a Person’s related enum besides it for code organization. You could then declare the Person class in a sub module so your enum is somewhat declared close to your class without being too public.

    You could also take a look at boost’s pickle support. However I haven’t tried it.

    A third solution could be to use something else than pickle to store your object.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written the following code: @Entity @Table(name=person) @Inheritance(strategy=InheritanceType.JOINED) public class Person { private
I have the following code: class Person { public String Name { get; set;
I have the following CoffeeScript code: class Person secret = 0 constructor: (@name, @age,
I have the following code: #!/usr/bin/ruby class Person def self.speak p = self.new puts
I have following code snippet: class ABC{ public: int a; void print(){cout<<hello<<endl;} }; int
I have following code public class TEST { public static void main(String arg[]){ try
I have the following code which has a mutable Person class, String and a
Say i have the following: public class Person{ public int ID {get; set;} public
I have the following classes: public class Person { [Key] public Guid Id {
I have the following code: public class Test { public static void Main() {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.