embed python: how do you call a class's attribute that is located inside another class...

Mike C. Fletcher mcfletch at rogers.com
Fri May 30 13:29:26 EDT 2003


Your Python code makes no sense (you want to "call" a string, and you're 
using the builtin list type's append method which wouldn't store 
anything in self.list (don't name things to shadow builtins where 
possible, BTW)), so it's hard to give you an answer.  Try posting the 
actual code you're using and explaining what you're trying to get a 
little more clearly. 

If you're really trying to get the class-attribute 'someValue' of your 
inner class:

    PyObject * bClass = PyObject_GetAttrString( aClass, "B");
    PyObject * someValue = PyObject_GetAttrString( bClass, "someValue").

But that doesn't appear to be what you're trying to do in your code.

If you're trying to get to the someValue attribute of an instance in a 
list attribute of an aClass instance, you'd do something along these lines:

    PyObject * myListOfBs = PyObject_GetAttrString( aInstance,
    "myListOfBs");
    PyObject * currentItem = PySequence_GetItem( myListOfBs, 0);
    PyObject * someValue = PyObject_GetAttrString( currentItem,
    "someValue");

In particular, look at the Python/C API reference manual, it gives docs 
on all of the normal mechanisms you'd use for this kind of work.

Note:

    The code above is all pseudo-code, you'd actually have to define the
    variables before-hand, and be checking for error conditions, and all
    the other evil things C makes you do :) .  You also would need to be
    keeping reference counts in mind.

HTH,
Mike


David Ang wrote:

>Anyone? pls help
>
>thanks.
>
>davidang at info.com.ph (David Ang) wrote in message news:<8b1c146b.0305290553.5ef4b32f at posting.google.com>...
>  
>
>>Hello,
>>
>>ie,
>>
>>class A
>>    self.list = []
>>    def somefunc
>>        b = self.B()
>>        ...
>>        list.append(b)
>>
>>    Class B
>>         self.someValue = ""
>>
>>
>>how do you exactly call B's self.someValue in C?
>>
>>i tried doing this but fail...
>>
>>PyObject *pObj = PyMapping_GetItemString(pDict, "someInstance");
>>PyObject_GetAttrString(pObj, "list[0].someValue") 
>>// returns -1
>>
>>many thanks.
>>
>>David
>>    
>>
_______________________________________
  Mike C. Fletcher
  Designer, VR Plumber, Coder
  http://members.rogers.com/mcfletch/








More information about the Python-list mailing list