Accessing members of Python class from C++ (Embedded Python)????

Discussion in 'Python' started by JM, Aug 27, 2003.

  1. JM

    JM Guest

    Hello everyone,

    Does anybody know about, have documentation on, or have any code
    samples on how to access class members from a python class in C++.

    Say I have a simple python script:

    ---------------------------
    class Animal:
    NumLegs = 5
    Size = 4.5
    ---------------------------

    How exactly do you access these members from C++. I know how to get
    the member if I know its name, ie.

    pkObject = PyDict_GetItemString(pkDict, "Animal");
    int uiNumLegs = PyInt_AsLong(PyObject_GetAttrString(pkObject,"NumLegs"));

    But how do you iterate through all the class members and print their
    names and values? This is easy to do for basic variables, but I can't
    seem to do it for class variables.

    Any help would be much apprecited. This is driving me nuts.

    JG
    JM, Aug 27, 2003
    #1
    1. Advertising

  2. JM

    JM Guest

    If anyone is interested in the solution:

    pDict=PyObject_GetAttrString(PyObject,"__dict__") will get the local
    symbol table (a dictionary) for the class.

    JG

    (JM) wrote in message news:<>...
    > Hello everyone,
    >
    > Does anybody know about, have documentation on, or have any code
    > samples on how to access class members from a python class in C++.
    >
    > Say I have a simple python script:
    >
    > ---------------------------
    > class Animal:
    > NumLegs = 5
    > Size = 4.5
    > ---------------------------
    >
    > How exactly do you access these members from C++. I know how to get
    > the member if I know its name, ie.
    >
    > pkObject = PyDict_GetItemString(pkDict, "Animal");
    > int uiNumLegs = PyInt_AsLong(PyObject_GetAttrString(pkObject,"NumLegs"));
    >
    > But how do you iterate through all the class members and print their
    > names and values? This is easy to do for basic variables, but I can't
    > seem to do it for class variables.
    >
    > Any help would be much apprecited. This is driving me nuts.
    >
    > JG
    JM, Aug 27, 2003
    #2
    1. Advertising

  3. JM

    Greg Chapman Guest

    On 27 Aug 2003 10:14:02 -0700, (JM) wrote:

    >If anyone is interested in the solution:
    >
    >pDict=PyObject_GetAttrString(PyObject,"__dict__") will get the local
    >symbol table (a dictionary) for the class.


    This may not be relevant to your needs, but a class's __dict__ only has
    references to the attributes actually declared in the class; it doesn't have
    inherited attributes. If you need access to all the attributes of a class, use
    PyObject_Dir(aClass), which returns a list of all attribute names declared in
    the class and it's superclasses (it's the C equivalent of the dir builtin). You
    can then use PyObject_GetAttr(aClass, attrname) to fetch the attributes
    themselves.

    ---
    Greg Chapman
    Greg Chapman, Aug 28, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bernd Fuhrmann
    Replies:
    3
    Views:
    411
    Roedy Green
    Oct 15, 2005
  2. John Harrison
    Replies:
    18
    Views:
    552
    Pete Becker
    Aug 11, 2003
  3. CoolPint
    Replies:
    8
    Views:
    973
    Jeff Schwab
    Dec 14, 2003
  4. hdixon
    Replies:
    3
    Views:
    637
    hdixon
    Jul 9, 2006
  5. Bhawna
    Replies:
    7
    Views:
    471
    Bhawna
    Aug 26, 2008
Loading...

Share This Page