Python Forum
creating arbitrary local variable names
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
creating arbitrary local variable names
#1
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?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
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?
Reply
#3
Use a dict, NamedTuple, Dataclass or other convenient data structures.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#4
(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'
Reply
#5
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).
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
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!
Reply
#7
(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
ndc85430 likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#8
"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].
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#9
(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.
buran and ndc85430 like this post
« We can solve any problem by introducing an extra level of indirection »
Reply
#10
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how solve: local variable referenced before assignment ? trix 5 1,763 Jun-15-2024, 07:15 PM
Last Post: trix
  It's saying my global variable is a local variable Radical 5 5,545 Oct-02-2023, 12:57 AM
Last Post: deanhystad
  Why doesn't calling a parent constructor work with arbitrary keyword arguments? PurposefulCoder 4 1,932 Jun-24-2023, 02:14 PM
Last Post: deanhystad
  UnboundLocalError: local variable 'wmi' referenced before assignment ilknurg 2 2,988 Feb-10-2022, 07:36 PM
Last Post: deanhystad
  [split] Creating a variable as a function DPaul 23 10,214 Sep-07-2020, 05:20 PM
Last Post: DPaul
  Creating a variable as a function JarredAwesome 4 3,814 Sep-06-2020, 05:08 AM
Last Post: buran
  Alternative to dynamic variable names catosp 13 6,688 Jun-20-2020, 03:45 PM
Last Post: catosp
  Help creating a variable as a result Realen 2 2,463 Jun-18-2020, 04:46 AM
Last Post: Realen
  UnboundLocalError: local variable 'figure_perso' referenced before assignment mederic39 2 3,080 Jun-11-2020, 12:45 PM
Last Post: Yoriz
  local variable 'marks' referenced before assignment Calli 3 3,132 May-25-2020, 03:15 PM
Last Post: Calli

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020