Posts: 4,654
Threads: 1,497
Joined: Sep 2016
i have a big block of code that sets up a couple dozen variables for later code to use. now i need to run this block 2 or 3 times. each time may or may not set individual variables. it might make sense to make the block be a function, but setting the variables doesn't happen in the caller's local name space. are there any workarounds or other ideas? this code is a project only i will use so the code doesn't need to be pythonic.
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 4,654
Threads: 1,497
Joined: Sep 2016
the caller will get about 30 different variables set by this block of code under the 2 different uses. so if done as a function it would be like side effects. in the original design, it was a block of code that sets 30 different variables. the new design needs to split up the block and run the whole thing twice. it can't just simply be put in a loop because there is a bunch of other code to do between those two runs of the big block. after both runs, all 30 variables are now set. if i make it as a function that returns locals() then i still need to get 30 items out of that dictionary and set those variables.
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 4,654
Threads: 1,497
Joined: Sep 2016
what is wrong with local variables?
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 4,654
Threads: 1,497
Joined: Sep 2016
that aspect of locals() is why i haven't used it to modify them in any serious way. i have tried to modify them and it does seem to work. but there could be cases where it doesn't work, ranging from the old value remains to a segfault of the engine or undetected data corruption, and numerous cases in between such how you explained it. compiled code could be caching recently accessed or correctly modified values. since it seems to allow read/fetch access from the dictionary, that suggests to me that the compiled code is expected to store these values.
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.