LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   create variables in python (https://www.linuxquestions.org/questions/programming-9/create-variables-in-python-557048/)

darkleaf 05-27-2007 03:03 PM

create variables in python
 
Hello,

I want to create new variables based on the length of a list.
for example if a = [1,2] I would like to create var1 and var2 but if the list is: b = [2,5,7] I want var1, var2 and var3 too. How would be the best way to create those variables cause I don't want to have a set number of variables.

Is this bad practice btw?

Thanks

uselpa 05-27-2007 05:11 PM

I've never seen that "style" in Python. What do you want to achieve?

ghostdog74 05-27-2007 06:32 PM

Quote:

Originally Posted by darkleaf
Hello,

I want to create new variables based on the length of a list.
for example if a = [1,2] I would like to create var1 and var2 but if the list is: b = [2,5,7] I want var1, var2 and var3 too. How would be the best way to create those variables cause I don't want to have a set number of variables.

Is this bad practice btw?

Thanks

Code:

>>> a = [1,2]
>>> for i in a:
...  locals()["%s" % "var"+str(i)]=i
...
>>> print var1
1

its not really bad, if your requirement is like this, you have no choice but to do it this way. But you must be careful not to "override" variables with the same time you already have.

darkleaf 05-28-2007 03:08 AM

Yes, thank you that's what I want to do. I don't know if it's the proper way but I'm also doing this to get to know a bit more about python. I think I used this method in secondary school but it was in visualbasic and I was interested if it would actually be possible to do something like that.

For the real problem, I have a list with variables and to each variable in that list I want to link a list with numbers. I want to create a tuple like this:

list[0], list0variables
list[1], list1variables

but because I can add items to the list I need to have a new list listvariables. It's also probably not the prettiest way to do it. I suppose in a database might be better but I like playing around a bit to see what python can do. And it's for personal use.

thanks!

uselpa 05-28-2007 04:59 AM

Quote:

Originally Posted by darkleaf
For the real problem, I have a list with variables and to each variable in that list I want to link a list with numbers. I want to create a tuple like this:

list[0], list0variables
list[1], list1variables

but because I can add items to the list I need to have a new list listvariables.

I still don't quite get it but it sounds like dictionnaries are the proper way to go here, i.e. dic[varname]=[var1,var2,...].

ghostdog74 05-28-2007 07:21 AM

Quote:

Originally Posted by uselpa
I still don't quite get it but it sounds like dictionnaries are the proper way to go here, i.e. dic[varname]=[var1,var2,...].

OP wants to create "dynamic" variables. it means you don't define it in the script beforehand. you create them as the script runs.

uselpa 05-28-2007 09:13 AM

Quote:

Originally Posted by ghostdog74
OP wants to create "dynamic" variables. it means you don't define it in the script beforehand. you create them as the script runs.

That's obvious, but I fail to see in how far that makes sense.

In any case, a local variable is always created in the local namespace, like this:
Code:

>>> locals()
{'__builtins__': <module '__builtin__' (built-in)>, '__name__': '__main__', '__doc__': None}
>>> locals()['a']=1
>>> locals()
{'__builtins__': <module '__builtin__' (built-in)>, '__name__': '__main__', '__doc__': None, 'a': 1}
>>> locals()['b']=(2,3)
>>> locals()
{'__builtins__': <module '__builtin__' (built-in)>, '__name__': '__main__', 'b': (2, 3), '__doc__': None, 'a': 1}
>>> locals()['a']
1
>>> locals()['b']
(2, 3)
>>> a
1
>>> b
(2, 3)

so a is the same as locals()['a'] and so on.

Using a dictionnary has the advantage that you have a dedicated container for your dynamic variables which is more convenient than creating the variables in the implicit locals() dictionnary.

Just my 2 cents.

darkleaf 05-28-2007 04:29 PM

But that would make me want to create the dictionary on the fly so it's sort of the same thing then. The dictionary is sort of what I want but it's not really about how I'm going to store the variables in the end, cause that'll be my own experimenting with it. That way I'll learn more, but more about creating new variables.

Thanks!


All times are GMT -5. The time now is 05:32 PM.