F
Frohnhofer, James
My initial problem was to initialize a bunch of dictionaries at the start of a
function.
I did not want to do
def fn():
a = {}
b = {}
c = {}
. . .
z = {}
simply because it was ugly and wasted screen space.
First I tried:
for x in (a,b,c,d,e,f,g): x = {}
which didn't work (but frankly I didn't really expect it to.)
Then I tried:
for x in ('a','b','c','d','e','f','g'): locals()[x]={}
which did what I wanted, in the interpreter. When I put it inside a function,
it doesn't seem to work. If I print locals() from inside the function, I can
see them, and they appear to be fine, but the first time I try to access one
of them I get a "NameError: global name 'a' is not defined"
Now obviously I could easily avoid this problem by just initializing each
dictionary, but is there something wrong about my understanding of locals,
that my function isn't behaving the way I expect?
==============================================================================
This message is for the sole use of the intended recipient. If you received
this message in error please delete it and notify us. If this message was
misdirected, CSFB does not waive any confidentiality or privilege. CSFB
retains and monitors electronic communications sent through its network.
Instructions transmitted over this system are not binding on CSFB until they
are confirmed by us. Message transmission is not guaranteed to be secure.
==============================================================================
function.
I did not want to do
def fn():
a = {}
b = {}
c = {}
. . .
z = {}
simply because it was ugly and wasted screen space.
First I tried:
for x in (a,b,c,d,e,f,g): x = {}
which didn't work (but frankly I didn't really expect it to.)
Then I tried:
for x in ('a','b','c','d','e','f','g'): locals()[x]={}
which did what I wanted, in the interpreter. When I put it inside a function,
it doesn't seem to work. If I print locals() from inside the function, I can
see them, and they appear to be fine, but the first time I try to access one
of them I get a "NameError: global name 'a' is not defined"
Now obviously I could easily avoid this problem by just initializing each
dictionary, but is there something wrong about my understanding of locals,
that my function isn't behaving the way I expect?
-----Original Message-----
From: [email protected]
[mailto[email protected]]On
Behalf Of Dennis Lee Bieber
Sent: Tuesday, November 09, 2004 10:31 AM
To: (e-mail address removed)
Subject: Re: Determining combination of bits
The dictionary was filled with arbitrary values, not
{ x : 2^x } values like you might have thought.
Well, you had stated "powers of two"... If all you wanted is a
bit mapping you could probably drop the dictionary and just use a list
of the values, indexed by the bit position, and my first attempt
logic...
It is actually more like {1:123, 2:664, 4:323, 8:990, 16:221... etc}
CheckBoxes = [ "FirstChoice",
"SecondChoice",
"ThirdChoice",
"FourthChoice",
"FifthChoice",
"SixthChoice" ]
for num in [22, 25, 9]:
bit = 0
while num:
if num & 1:
print CheckBoxes[bit],
bit = bit + 1
num = num >> 1
SecondChoice ThirdChoice FifthChoice
FirstChoice FourthChoice FifthChoice
FirstChoice FourthChoice
where "num" is the sum of the checkbox index values (or whatever
selection mechanism is used), assuming /they/ were set up in 2^(n+1)
scheme (n = bit position, starting with 0)...
--============================================================== <
(e-mail address removed) | Wulfraed Dennis Lee Bieber KD6MOG <
(e-mail address removed) | Bestiaria Support Staff <
============================================================== <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <
==============================================================================
This message is for the sole use of the intended recipient. If you received
this message in error please delete it and notify us. If this message was
misdirected, CSFB does not waive any confidentiality or privilege. CSFB
retains and monitors electronic communications sent through its network.
Instructions transmitted over this system are not binding on CSFB until they
are confirmed by us. Message transmission is not guaranteed to be secure.
==============================================================================