Iterator for Vector of objects returns copy of Object (Python)

View: New views
3 Messages — Rating Filter:   Alert me  

Iterator for Vector of objects returns copy of Object (Python)

by Jose Luna-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I am using the std::vector template to contain a set of objects.

%include "std_vector.i"
namespace std {
   %template(vector_cgv) vector<CGVertex>;
};

Everything works fine when I bring the vector into Python, but when I iterate through the vector it returns a copy of the object instead of the object itself.  For example, if vertices is my vector:

for vertex in vertices:
    vertex.tmp = 1
print vertex[0].tmp

This code will cause the following error: "AttributeError: tmp".  The tmp attribute assigns fine, but it's assigning it to a copy of the vertex object, so when I try to go back and access that attribute it isn't there.  (I've confirmed this through further testing.) I realize that I could simply build a new list of objects, but that would be inefficient and I'm hoping for a more elegant solution.

Thanks in advance,

JLuna






      ____________________________________________________________________________________
Be a better friend, newshound, and
know-it-all with Yahoo! Mobile.  Try it now.  http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ

-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference
Don't miss this year's exciting event. There's still time to save $100.
Use priority code J8TL2D2.
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user

Re: Iterator for Vector of objects returns copy of Object (Python)

by Josh Cherry :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message



On Thu, 1 May 2008, Jose Luna wrote:

> I am using the std::vector template to contain a set of objects.
>
> %include "std_vector.i"
> namespace std {
>   %template(vector_cgv) vector<CGVertex>;
> };
>
> Everything works fine when I bring the vector into Python, but when I
> iterate through the vector it returns a copy of the object instead of
> the object itself.  For example, if vertices is my vector:
>
> for vertex in vertices:
>    vertex.tmp = 1
> print vertex[0].tmp
>
> This code will cause the following error: "AttributeError: tmp".  The
> tmp attribute assigns fine, but it's assigning it to a copy of the
> vertex object, so when I try to go back and access that attribute it
> isn't there.  (I've confirmed this through further testing.) I realize
> that I could simply build a new list of objects, but that would be
> inefficient and I'm hoping for a more elegant solution.

I'm not sure I understand.  Is tmp a member of the C++ class, or is it
something you add to the Python object by that assignment?  If it's just
being added to the Python object, of course it won't affect the underlying
C++ object (where would the data go?) an the print statement will fail.

Josh


-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference
Don't miss this year's exciting event. There's still time to save $100.
Use priority code J8TL2D2.
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user

Parent Message unknown Re: Iterator for Vector of objects returns copy of Object (Python)

by Jose Luna-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> I'm not sure I understand.  Is tmp a member of the C++ class, or is it
> something you add to the Python object by that assignment?  If it's just
> being added to the Python object, of course it won't affect the underlying
> C++ object (where would the data go?) an the print statement will fail.

Oops, I forgot to reply to the list.  This issue is now resolved.  I thought the vector container held proxy class objects, but instead it holds the actual C++ object and only wraps it in a proxy class when you request it from the vector.  Thanks for the help.

JLuna






      ____________________________________________________________________________________
Be a better friend, newshound, and
know-it-all with Yahoo! Mobile.  Try it now.  http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ

-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference
Don't miss this year's exciting event. There's still time to save $100.
Use priority code J8TL2D2.
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user