changing a value of a variable

François Pinard pinard at iro.umontreal.ca
Tue Jan 29 20:09:39 EST 2002


[Marcin Matuszkiewicz]

> How to write a python program that accomplishes the same thing as the
> C program below.

> void assign(int *x)
> {
>   *x = 1;
> }

> int main(void)
> {
>    int n = 0;

>    /* n == 0 */
>    assign(n);
>    /* n == 1 */

>    return 0;
> }

Hello, Marcin.  As others told you, we prefer avoiding such things in Python.
But if you just cannot escape it, you may do stunts like this one:

    def assign(x):
        x[0] = 1

    def main():
        n = 0
        p = [n]
        assign(p)
        n = p[0]

Once again, it is usually fruitful trying to use other thinking lines.

-- 
François Pinard   http://www.iro.umontreal.ca/~pinard




More information about the Python-list mailing list