Dynamic variable creation from string
Terry Reedy
tjreedy at udel.edu
Wed Dec 7 19:27:43 EST 2011
On 12/7/2011 7:03 PM, Steven D'Aprano wrote:
> On Wed, 07 Dec 2011 09:09:16 -0800, Massi wrote:
>
>> Is there a way to create three variables dynamically inside Sum in order
>> to re write the function like this?
I should have mentioned in my earlier response that 'variable' is a bit
vague and misleading. Python has names bound to objects.
>> def Sum(D) :
>> # Here some magic to create a,b,c from D
>> return a+b+c
>
> No magic is needed.
>
> a, b, c = D['a'], D['b'], D['c']
This is not what most people mean by 'dynamically created variables'.
The names are static, in the code, before the code is executed.
In 2.x, 'from x import *' dynamically creates local names that are not
in the code that contains the import. Dynamically creating objects is
what Python code does all the time.
--
Terry Jan Reedy
More information about the Python-list
mailing list