The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
dynamic variable declaration
Discuss dynamic variable declaration in the Python Programming forum on Dev Shed. dynamic variable declaration Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

April 17th, 2004, 09:37 AM
|
|
Registered User
|
|
Join Date: Apr 2004
Posts: 2
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
dynamic variable declaration
hi ,
how can i declare variables dynamically?
for example , in php i can do
$var_num = 1;
${'var'.$var_num} = 5;
so i get
$var1 = 5
how can i do that in python?
thanks 
|

April 17th, 2004, 10:52 AM
|
|
Contributing User
|
|
Join Date: Jul 2003
Posts: 133
Time spent in forums: < 1 sec
Reputation Power: 10
|
|
If I understood what you wanted, this can be done with:
Code:
setattr(object, name, value)
Set a named attribute on an object; setattr(x, 'y', v) is equivalent to
``x.y = v''.
|

April 17th, 2004, 11:25 AM
|
|
Registered User
|
|
Join Date: Apr 2004
Posts: 2
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
yes, that's what i need , thanks man 
|

April 17th, 2004, 12:57 PM
|
|
Contributing User
|
|
Join Date: Feb 2004
Location: London, England
|
|
The solution that percivall gave will set an attribute on an object, as he said. To create a local or global variable (as in your PHP example) you can use the builtin functions locals() and globals(), which will return the dictionaries used for those namespaces. If you are in the top level namespace (i.e. outside of a function or class) then the two are equivalent.
The Python code corresponding to your example is:
Code:
>>> var_num = 1
>>> globals()['var%s' % var_num] = 5
>>> var1
5
However the docs warn that modifying the locals() dictionary may not always work and should be avoided. It worked OK when I tested it, but may fail in some circumstances. An alternative that will always work is to use exec:
Code:
>>> exec 'var%s = 23' % var_num
>>> var1
23
Dave - The Developers' Coach
|

April 17th, 2004, 01:12 PM
|
|
Contributing User
|
|
Join Date: Feb 2004
Location: London, England
|
|
I said:
Quote: | Originally Posted by DevCoach However the docs warn that modifying the locals() dictionary may not always work and should be avoided. It worked OK when I tested it, but may fail in some circumstances. |
After a little more experimenting and research, I have found that modifying locals() works ok at the top level namespace since it then locals() and globals() are the same. Inside a function locals() returns a copy of the namespace, so any changes to it do not affect the local variables:
Code:
>>> var1 = 0
>>> def fn():
... var_num = 1
... locals()['var%s' % var_num] = 6
... print var1
...
>>> fn()
0
so use either globals() or exec, depending on your requirements.
Dave - The Developers' Coach
|

April 17th, 2004, 04:58 PM
|
|
Contributing User
|
|
Join Date: Dec 2001
Location: Houston, TX
Posts: 383
Time spent in forums: 1 h 41 m 27 sec
Reputation Power: 12
|
|
|
A better solution is, of course, to not do this at all but instead to use a list of variables.
|

April 23rd, 2004, 01:38 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
|
I agree with Strike here although the topic itself is very intersting - i saw a similar question in the perl forum ages ago and the recomendation there was also to use an array (list) - the best way to go IMO.
Mark.
__________________
programming language development: www.netytan.com – Hula
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|