Python Forum
Generate Python variables dynamically - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Generate Python variables dynamically (/thread-36546.html)



Generate Python variables dynamically - Pedroski55 - Mar-02-2022

For getting data from MySQL and displaying it in my output.php, I dynamically create PHP variables

Quote:$i = 0;
$j = 0;
foreach($allQsdata as $Qsdata) {
++$i;
$Qnr = 'Q' . $i;
${'Q'.$i} = $questions[$j];
++$j;
// other stuff
}

The I have PHP variables like: $Q1 , $Q2 ... These arre then passed to a function as the offset names of the data arrays collected from MySQL

I was just wondering, is it possible to dynamically create variables in Python? Just out of interest.


RE: Generate Python variables dynamically - perfringo - Mar-02-2022

Suggested reading: Why you don't want to dynamically create variables


RE: Generate Python variables dynamically - deanhystad - Mar-03-2022

If you have an object you can set attributes for the object
import junk

for name, value in zip('abc', (1, 2, 3)):
    setattr(junk, name, value)

print(junk.a, junk.b, junk.c)
Output:
1 2 3
What I don't know is how to get the module for the file you are in. I suppose you could use locals() or globals().
globals()['a'] = "I am a global a"
locals()['b'] = "I am a global b"

def func():
    locals()['b'] = "I am local b"
    locals()['c'] = "I am local c"
    print(a)
    print(b)
    print(c)

print(a)
print(b)

func()
Output:
I am a global a I am a global b I am a global a I am a global b Traceback (most recent call last): File "...", line 14, in <module> func() File "...", line 9, in func print(c) NameError: name 'c' is not defined
Looks like locals() doesn't create variables in the local scope of the function. Not surprising.

I cannot fathom why you would ever want to do this. A variable is a convenient name you can use to access an object. Dynamically created names are not going to be convenient.


RE: Generate Python variables dynamically - ibreeden - Mar-03-2022

(Mar-02-2022, 10:51 PM)Pedroski55 Wrote: I was just wondering, is it possible to dynamically create variables in Python? Just out of interest.

Don't go that way. And why should you?
(Mar-02-2022, 10:51 PM)Pedroski55 Wrote:
${'Q'.$i} = $questions[$j];

Why not use a dictionary?
Q = {}
...
Q[i] = questions[j]



RE: Generate Python variables dynamically - Pedroski55 - Mar-04-2022

Thanks for the replies, interesting.

All I can say is, in PHP, I have found this very useful, since I have a function which returns an array with contents from MySQL and I need to collect X different arrays, depending on the number of questions, which varies. Barring writing static individual code for each week (PHP and me are not good friends), dynamically created variables are very useful!


RE: Generate Python variables dynamically - deanhystad - Mar-04-2022

If you think you need to create variables it just means you don't understand collections. If you really think about it, there are no variables in Python. a = a + 1 is locals["a"] = locals["a"] + 1
a = 1
print(a)
a = a  + 1
print(a)
locals()["a"] = locals()["a"] + 1
print(a, locals()["a"])
Output:
1 2 3 3
So anything you can do by dynamically making variables you could do with a dictionary. And a dictionary is better because it will only contain the keys you add. It not only keeps my things organized and together, it keeps everything else out. You can iterate through a dictionary of your PHP arrays and be comfortable knowing that all the items are PHP arrays. You also know you didn't accidentally overwrite something in the local scope.

I don't think dynamically created variables are bad. They are much worse than that.