Python Args By Reference

Terry Reedy tjreedy at udel.edu
Wed May 11 19:57:20 EDT 2005


"ncf" <nothingcanfulfill at gmail.com> wrote in message 
news:1115787330.723188.295610 at f14g2000cwb.googlegroups.com...
> Example C Code:
> #define P(a,b,c,d,e,f,g,h,x,K) \
> { \
> temp1 = h + S3(e) + F1(e,f,g) + K + x; \
> temp2 = S2(a) + F0(a,b,c); \
> d += temp1; h = temp1 + temp2; \
> }

> Python Code:
> def P(a,b,c,d,e,f,g,h,x,K):
>    temp1 = h + S3(e) + F1(e,f,g) + K + x
>    temp2 = S2(a) + F0(a,b,c)
>    d += temp1; h = temp1 + temp2

Your problem is that you are replacing a C macro, which is inlined and 
affects the original 'namespace' in which it appears, and which does not 
have a direct Python equivalent, with a Python function (admittedly 
slower), which performs its calculation in a separate namespace.  So you 
have to pass the results back to the calling namespace.  Simple enough: 
make the last line of P

     return d+temp1, temp1+temp2

and the call

    a4,a8 = P(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)

The stuff about mutability and calling conventions is important to learn 
eventually but seems beside the point for your immediate application.

Terry J. Reedy






More information about the Python-list mailing list