Hello all, I was wondering if there was any way to pass arguments
(integer and such) by reference (address of), rather than by value.
Many thanks in advance.
-Wes 20 1766
On 2005-05-10, ncf <no***************@gmail.com> wrote: Hello all, I was wondering if there was any way to pass arguments (integer and such) by reference (address of), rather than by value.
Yes. All arguments are passed by reference. This must be in
the FAQ somewhere...
Sure enough. 30 seconds of browsing the FAQ yielded this: http://www.python.org/doc/faq/progra...l-by-reference
--
Grant Edwards grante Yow! Wow! Look!! A stray
at meatball!! Let's interview
visi.com it!
ncf wrote: Hello all, I was wondering if there was any way to pass arguments (integer and such) by reference (address of), rather than by value.
Many thanks in advance.
-Wes
All mutable types in python are passed by reference automatically.
Joseph Garvin wrote: ncf wrote:
Hello all, I was wondering if there was any way to pass arguments (integer and such) by reference (address of), rather than by value.
Many thanks in advance.
All mutable types in python are passed by reference automatically.
More accurately:
(1) All function arguments are passed by value, but
(2) All "values" are pointers.
Statement (2) also explains why a = [1, 2, 4] b = a b[1] = 0 print a
[1, 0, 4]
behaves the way it does.
And there's no difference between mutable and immutable types. It's
just that with immutable types, it doesn't matter how they're passed.
On 2005-05-10, Joseph Garvin <k0*****@kzoo.edu> wrote: ncf wrote:
Hello all, I was wondering if there was any way to pass arguments (integer and such) by reference (address of), rather than by value.
All mutable types in python are passed by reference automatically.
So are immutable ones. It just doesn't matter in that case.
--
Grant Edwards grante Yow! "DARK SHADOWS"
at is on!! Hey, I think
visi.com the VAMPIRE forgot his
UMBRELLA!!
Ok, I'm relatively new to python (< 1 year experiance), yet I have had
much experiance with other languages.
I never really looked that deeply in the FAQ -- temporary lapse of
stupidity(?). Thanks for the link, the concept seems to help.
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.
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
The reason why it'd be a pain to implement this by any of the methods
provided in the Python FAQs is that SHA-256 rotates the variable order
in the calls. Example code:
P( A, B, C, D, E, F, G, H, W[ 0], 0x428A2F98 );
P( H, A, B, C, D, E, F, G, W[ 1], 0x71374491 );
P( G, H, A, B, C, D, E, F, W[ 2], 0xB5C0FBCF );
P( F, G, H, A, B, C, D, E, W[ 3], 0xE9B5DBA5 );
P( E, F, G, H, A, B, C, D, W[ 4], 0x3956C25B );
P( D, E, F, G, H, A, B, C, W[ 5], 0x59F111F1 );
P( C, D, E, F, G, H, A, B, W[ 6], 0x923F82A4 );
P( B, C, D, E, F, G, H, A, W[ 7], 0xAB1C5ED5 );
P( A, B, C, D, E, F, G, H, W[ 8], 0xD807AA98 );
Since I cannot simply do it the way I had originally seen it, would
there be an alternative method to proforming the operations that I am
missing?
Once again, many thanks for your time.
-Wes
"ncf" <no***************@gmail.com> writes: Since I cannot simply do it the way I had originally seen it, would there be an alternative method to proforming the operations that I am missing?
Use an array instead of all those named variables.
As I fail to see how an array could be used in this (my own
stupidity?), would you have any such example? For reference, I'm trying
to translate this: http://www.cr0.net:8040/code/crypto/sha256/ (Inside
sha256_process).
Once again, thanks for the patience, I'm still picking up on all the
little tid-bits. :)
-Wes
"ncf" <no***************@gmail.com> writes: As I fail to see how an array could be used in this (my own stupidity?), would you have any such example?
How's this (untested):
state = [A,B,C,D,E,F,G,H]
magic = [0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5,
0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5, 0xD807AA98 ]
def P(state, i, magic):
a,b,c,d,e,f,g,h = state[i:] + state[:i]
temp1 = h + S3(e) + F1(e,f,g) + K + x
temp2 = S2(a) + F0(a,b,c)
# d += temp1; h = temp1 + temp2
state[(i+3)%8] += temp1; state[(i+7)%8] = temp1 + temp2
for i in range(9):
P(state, i, W, magic)
Actually this isn't so good. You have to be careful about arithmetic
overflow, i.e. do all the additions mod 2**32, so you don't get Python
longs. I'll leave that as an exercise.
Paul Rubin <http://ph****@NOSPAM.invalid> writes: state = [A,B,C,D,E,F,G,H] magic = [0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5, 0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5, 0xD807AA98 ]
def P(state, i, magic): a,b,c,d,e,f,g,h = state[i:] + state[:i] temp1 = h + S3(e) + F1(e,f,g) + K + x
Woops, K is supposed to say magic[i].
ncf wrote: As I fail to see how an array could be used in this (my own stupidity?), would you have any such example? For reference, I'm
trying to translate this: http://www.cr0.net:8040/code/crypto/sha256/
(Inside sha256_process).
Once again, thanks for the patience, I'm still picking up on all the little tid-bits. :)
def P(arr, x, k):
a, b, c, d, e, f, g, h = arr
temp1 = arr[7] + S3(arr[4]) + F1(*arr[4:7]) + k + x
temp2 = S2(arr[0]) + F0(*arr[:3])
arr[3] += temp1
arr[7] = temp1 + temp2
K = [0x428A2F98, 0x71374491, 0xB5C0FBCF,
0xE9B5DBA5, 0x3956C25B, 0x59F111F1,
0x923F82A4, 0xAB1C5ED5, 0xD807AA98]
....
for i in xrange(9):
P(arr, w[i], K[i])
if i != 8: # don't rotate the last time
arr = [arr[-1]] + arr[:-1] # rotate arr
The last line moves the last element of arr to the beginning (so the
positions are correct for the call to P).
Quoth "ncf" <no***************@gmail.com>:
....
| 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.
It's often introduced in a confusing way. We just say it's mutable
if it has one or more functions that modify its contents. There
isn't any other distinction, any other difference than that.
| 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
|
| The reason why it'd be a pain to implement this by any of the methods
| provided in the Python FAQs is that SHA-256 rotates the variable order
| in the calls. Example code:
| P( A, B, C, D, E, F, G, H, W[ 0], 0x428A2F98 );
| P( H, A, B, C, D, E, F, G, W[ 1], 0x71374491 );
| P( G, H, A, B, C, D, E, F, W[ 2], 0xB5C0FBCF );
| P( F, G, H, A, B, C, D, E, W[ 3], 0xE9B5DBA5 );
| P( E, F, G, H, A, B, C, D, W[ 4], 0x3956C25B );
| P( D, E, F, G, H, A, B, C, W[ 5], 0x59F111F1 );
| P( C, D, E, F, G, H, A, B, W[ 6], 0x923F82A4 );
| P( B, C, D, E, F, G, H, A, W[ 7], 0xAB1C5ED5 );
| P( A, B, C, D, E, F, G, H, W[ 8], 0xD807AA98 );
|
| Since I cannot simply do it the way I had originally seen it, would
| there be an alternative method to proforming the operations that I am
| missing?
I could be missing something, but I think you just have to suck it up
and do it one of the other two ways:
1. The more or less functional way: the function doesn't
have any side effects. It just returns the values it
computes, and it's for the caller to bind them to variables.
D, H = P(A, B, C, D, E, F, G, H, W[0], 0x428A2F98)
C, G = (H, A, B, C, D, E, F, G, W[1], 0x71374491)
...
2. The OOP way: each object has the appropriate function
that modifies its content value. That looks like a
big waste of time to me, but I mention it for conceptual
completeness.
Donn Cave, do**@drizzle.com
"ncf" <no***************@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 )
Roy Smith wrote: 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.
And ints. Strings, tuples and ints are the *three* most common
immutable objects you'll see...
-Peter
On 2005-05-11, Roy Smith <ro*@panix.com> wrote: The most common immutable objects you'll see are strings and tuples,
And integers. They're immutable, aren't they? At least the
small ones. And floats.
--
Grant Edwards grante Yow! .. bleakness....
at desolation.... plastic
visi.com forks...
On 2005-05-11, Peter Hansen <pe***@engcorp.com> wrote: 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.
And ints. Strings, tuples and ints are the *three* most common immutable objects you'll see...
Right. In Python, strings tuples and ints are the five most
common immutable objects you'll see.
--
Grant Edwards grante Yow! You were s'posed
at to laugh!
visi.com
Thanks to everyone for your assistance. I shall reread this a couple
times and then try to make something that works.
Many thanks and have a GREAT day.
-Wes
On Wednesday 11 May 2005 08:43 am, Peter Hansen wrote: Roy Smith wrote: 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.
And ints. Strings, tuples and ints are the *three* most common immutable objects you'll see...
And floats. Floats, strings, tuples, and ints art the *four* most common
immutable objects you'll see...
Time to break out the comfy chair. ;-)
--
Terry Hancock ( hancock at anansispaceworks.com )
Anansi Spaceworks http://www.anansispaceworks.com
"ncf" <no***************@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Brandon J. Van Every |
last post by:
What's better about Ruby than Python? I'm sure there's something. What is
it?
This is not a troll. I'm language shopping and I want people's answers. I
don't know beans about Ruby or have...
|
by: ajikoe |
last post by:
Hi,
I tried to follow the example in swig homepage.
I found error which I don't understand.
I use bcc32, I already include directory where my python.h exist in
bcc32.cfg.
/* File : example.c...
|
by: Kurt B. Kaiser |
last post by:
Patch / Bug Summary
___________________
Patches : 378 open ( +3) / 3298 closed (+34) / 3676 total (+37)
Bugs : 886 open (-24) / 5926 closed (+75) / 6812 total (+51)
RFE : 224 open...
|
by: cgwalters |
last post by:
Hi,
I've recently been working on an application which does quite a bit
of searching through large data structures and string matching, and I
was thinking that it would help to put some of this...
|
by: Benke |
last post by:
Hello, I'm quite new to Python and embedding python in c++. I'm trying to write a function that i can use to call a python function. It should take 3 arguments, the name of the python file, the...
|
by: Scott SA |
last post by:
On 5/1/08, Brian Vanderburg II (BrianVanderburg2@aim.com) wrote:
Did you review this?
<http://pydispatcher.sourceforge.net/>
from what I understand is originally based upon this:...
|
by: Tim Spens |
last post by:
The following is a simple complete example using the c python api to generate callbacks from c to python. But when I run the c code I get a segfault in PyInt_FromLong () (see below). Most of this...
|
by: =?UTF-8?B?TmlscyBPbGl2ZXIgS3LDtmdlcg==?= |
last post by:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hi,
I don't think the global interpreter lock is what you need ... read here
for reference:
http://docs.python.org/api/threads.html
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |