LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-27-2007, 03:03 PM   #1
darkleaf
Senior Member
 
Registered: Jun 2004
Location: the Netherlands
Distribution: debian SID
Posts: 2,170

Rep: Reputation: 45
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
 
Old 05-27-2007, 05:11 PM   #2
uselpa
Senior Member
 
Registered: Oct 2004
Location: Luxemburg
Distribution: Slackware, OS X
Posts: 1,507

Rep: Reputation: 46
I've never seen that "style" in Python. What do you want to achieve?
 
Old 05-27-2007, 06:32 PM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
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.

Last edited by ghostdog74; 05-27-2007 at 06:33 PM.
 
Old 05-28-2007, 03:08 AM   #4
darkleaf
Senior Member
 
Registered: Jun 2004
Location: the Netherlands
Distribution: debian SID
Posts: 2,170

Original Poster
Rep: Reputation: 45
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!
 
Old 05-28-2007, 04:59 AM   #5
uselpa
Senior Member
 
Registered: Oct 2004
Location: Luxemburg
Distribution: Slackware, OS X
Posts: 1,507

Rep: Reputation: 46
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,...].
 
Old 05-28-2007, 07:21 AM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
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.
 
Old 05-28-2007, 09:13 AM   #7
uselpa
Senior Member
 
Registered: Oct 2004
Location: Luxemburg
Distribution: Slackware, OS X
Posts: 1,507

Rep: Reputation: 46
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.
 
Old 05-28-2007, 04:29 PM   #8
darkleaf
Senior Member
 
Registered: Jun 2004
Location: the Netherlands
Distribution: debian SID
Posts: 2,170

Original Poster
Rep: Reputation: 45
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!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Source Bash variables into Python environ. hepburnenthorpe Programming 1 05-07-2007 10:57 AM
Threads synchronisation problem (mutex variables and contitional variables) zahadumy Programming 6 12-07-2005 12:30 PM
bizarre python behavior - simple cgi variables SerfurJ Programming 2 04-14-2004 02:58 PM
Shel scripting: variables pointing to variables and case Dark_Helmet Programming 5 06-08-2003 11:07 AM
Using letters/strings to assign variables in python Colonel Panic Programming 0 09-14-2001 10:13 PM


All times are GMT -5. The time now is 08:57 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration