changing a value of a variable

Aahz Maruch aahz at panix.com
Tue Jan 29 23:49:10 EST 2002


In article <mailman.1012353050.29181.python-list at python.org>,
=?iso-8859-1?q?Fran=E7ois?= Pinard <pinard at iro.umontreal.ca> wrote:
>[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]

Correct me if I'm wrong, but I believe that is not in any way equivalent
to the C code above.  Not being a C programmer, I'm not absolutely
positive, but I think you need to use assign(&n) in order for the code
to work.
-- 
                      --- Aahz  <*>  (Copyright 2002 by aahz at pobox.com)

Hugs and backrubs -- I break Rule 6                 http://www.rahul.net/aahz/
Androgynous poly kinky vanilla queer het Pythonista   

"I got the new SPORT-UTILITY Segway."  --Tom Toles



More information about the Python-list mailing list