call by reference (c module)
Michael Hudson
mwh at python.net
Wed Nov 13 06:16:28 EST 2002
Brian Lee <senux at senux.com> writes:
> Hi, I am newbie!
>
> (1) call by reference
>
> I am trying to make a simple C module. Follow is a brief example
> for my situation. How can I use `call by reference' to put argument
> value to C module?
>
> a = 1
> import some_c_module
> some_c_module.function(a) # function change variable a to 2
> print a # I want a to be 2 (not 1)
Can't be done. You could try reading one of these pages:
http://effbot.org/guides/python-objects.htm
http://starship.python.net/crew/mwh/hacks/objectthink.html
to get some understanding of why.
Actually, that's a slight lie; but
((PyIntObject*)ob)->ob_ival++;
is a *REALLY* bad idea: you would then have
>>> a = 1
>>> import some_c_module
>>> some_c_module.function(a) # function change variable a to 2
>>> print a
2
>>> 1
2 # oops!
don't even think of doing this -- I only mention this to discourage
you, so if you hadn't thought of this, forget the last few lines of my
post...
> (2) un-fixed size of arguments to a C module.
>
> Is there any (easy) way to put un-fixed size of arguments to a
> C module? I tested PyArg_ParseTuple() but I don't know how to
> manage un-fixed size of values with it.
Optional arguments are easy; just use the | character in the arglist
parameter to PyArg_ParseTuple.
For more flexible behaviour, noone says you have to call
PyArg_ParseTuple... it's just a convenience function.
Cheers,
M.
--
My hat is lined with tinfoil for protection in the unlikely event
that the droid gets his PowerPoint presentation working.
-- Alan W. Frame, alt.sysadmin.recovery
More information about the Python-list
mailing list