Python Args By Reference

Roy Smith roy at panix.com
Wed May 11 06:42:39 EDT 2005


"ncf" <nothingcanfulfill at gmail.com> wrote:
> The two issues I am having in grasping all of this are as follows:
> 1) Being new to Python, terms like mutable and immutable. Although I
> have probably dealt with them in other languages, the terms by
> themselves are a little confusing, but managable overall, so this issue
> isn't so big.

If you know C++, imagine a class where every method is declared const.  
That's essentially what Python's immutable means.  Once you've created an 
object, you can never modify it (other than to destroy it).

The most common immutable objects you'll see are strings and tuples, and 
the main reason they're immutable is to allow them to be dict keys.

> 2) LARGELY, my issue is as demonstrated by the following code. I was
> trying to accomplish an effect similar to what is possible in C.
> (Trying to make a pure-python FIPS-180-2 compliant implementation of
> the SHA-256 algorithm for more Python practice and to put into some
> code for a *trial* secure protocol.)
> 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

Perhaps something like this:

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)
    return (d+temp1, temp1+temp2)

then you could call it as:

D, H = P( A, B, C, D, E, F, G, H, W[ 0], 0x428A2F98 )
D, H = P( H, A, B, C, D, E, F, G, W[ 1], 0x71374491 )
D, H = P( G, H, A, B, C, D, E, F, W[ 2], 0xB5C0FBCF )
D, H = P( F, G, H, A, B, C, D, E, W[ 3], 0xE9B5DBA5 )
D, H = P( E, F, G, H, A, B, C, D, W[ 4], 0x3956C25B )
D, H = P( D, E, F, G, H, A, B, C, W[ 5], 0x59F111F1 )
D, H = P( C, D, E, F, G, H, A, B, W[ 6], 0x923F82A4 )
D, H = P( B, C, D, E, F, G, H, A, W[ 7], 0xAB1C5ED5 )
D, H = P( A, B, C, D, E, F, G, H, W[ 8], 0xD807AA98 )



More information about the Python-list mailing list