Python Forum

Full Version: creating arbitrary local variable names
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
i have some code that creates a bunch of variable names with their intended values. the names are formed in strings. is eval() safe to get these all assigned in the local namespace as their names and values are created?
Using eval() for something like this seems odd. It's obviously safe to use eval() if you provide all the strings to evaluate, but why use eval at all? You would be writing python code to write python code. This would make sense for some sort of adaptive algorithm, but I don't see it as the preferred method for assigning values to variables.

And why are you making a bunch of variable names?
Use a dict, NamedTuple, Dataclass or other convenient data structures.
(Aug-25-2024, 11:02 PM)Skaperen Wrote: [ -> ]i have some code that creates a bunch of variable names with their intended values
As mention use a data structure,when make a variables Python will store it in dictionary globals().
globals() should not be used as it store a lot other stuff,not just variables made.
>>> a = 100
>>> b = 'hello'    
>>> globals()['b']
'hello'
Just make a data structure that is visible.
from dataclasses import dataclass

@dataclass
class GlobData:
    a: int
    b: str

glob_data = GlobData(a=100, b='hello')
>>> glob_data.a
100
>>> glob_data.b
'hello'
i took my original problem and re-expressed it as a problem setting local variables (before i asked here). doing this as in a dict had a complication that doing things as locals would solve. i'll need to go back to the original problem and try it again as a dict. a way to set an arbitrary named local would have solved it. but Python seems to resist that (though globals seem OK).
Easy enough in PHP:

Quote:for ($i = 1; $i <= 10; $i++) {
${"variable" . $i} = "Value " . $i;
}

But I don't think this can be done in Python!
(Aug-29-2024, 02:41 AM)Skaperen Wrote: [ -> ]doing this as in a dict had a complication that doing things as locals would solve.

Looks like clear XY problem
"storing arbitrary variable names in local space" is X in an XY problem? i came up with what i thought was a solution, but a small part of it had a problem. my posted question was to see if that small part had a solution. apparently not and my interim solution it is a part of can't be viable. back to square [0][0].
(Aug-31-2024, 02:21 AM)Skaperen Wrote: [ -> ]my posted question was to see if that small part had a solution. apparently not
The main question is why do you want to create automatically a bunch of variable names? This is the unusual part, so tell us really why you need this. That's where you tell us your solution instead of telling us your problem.
it's apparently not a solution. a dict seems to be the way to go. i will just have to automatically translate code in a different way (to use an indexed dict instead of a variable name, everywhere such variables would have been). there will be a mix of code sources in this. first thoughts on design are being kicked out.