[C++-sig] Re: Returning reference to member variable that's a basic type
Joel Gerard
llywelyn.geo at yahoo.com
Mon Oct 6 19:09:14 CEST 2003
Hmmm... I see what you mean. Couldn't you map an int reference to something like:
class IntegerReference :
def __setattr__(self, name, value) :
#BPL code here
def __getattr__ (self, name) :
#BPL code here
Then you could do something like:
i = Obj.FnThatReturnsAnIntRef()
i.value = 45
IntegerReference would keep a pointer to the original integer (I'm assuming you can do this: keep
the address of the original variable as a python int or something, and then deference it in C?).
Then set and get would deference the pointer, and get/set the value respectively.
It's a pity that one can't overload the assignment operator in python (not that I've seen anyhow).
The "i.value" mechanism is not the cleanest, and I'd prefer to do "i=5" but I'd rather have some
mechanism than nothing. Perhaps there is a better way. I fairly new to python.
Thanks for the quick response. ;)
Joel
--- David Abrahams <dave at boost-consulting.com> wrote:
> Joel Gerard <llywelyn.geo at yahoo.com> writes:
>
> > I hope this doesn't get me the dumb question of the day award but...
> >
> > How do you return a pointer/reference to a member variable(a basic type) and modify it from
> > python?
>
> You don't. Basic types get mapped to Python ints, longs, and
> strings, all of which are immutable. YOu can't modify them.
>
> Decide what interface you'd like to see for this code in Python, code
> up a pure-Python mock-up which does what you want, and we can discuss
> how to achieve it with BPL.
>
> --
> Dave Abrahams
> Boost Consulting
> www.boost-consulting.com
>
>
> _______________________________________________
> C++-sig mailing list
> C++-sig at python.org
> http://mail.python.org/mailman/listinfo/c++-sig
=====
__________________________________
Do you Yahoo!?
The New Yahoo! Shopping - with improved product search
http://shopping.yahoo.com
More information about the Cplusplus-sig
mailing list