Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
Python
Concise idiom to initialize dictionaries
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Frohnhofer, James, post: 1788896"] 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. ============================================================================== [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Python
Concise idiom to initialize dictionaries
Top