On Wed, 07 Dec 2011 19:27:43 -0500, Terry Reedy wrote: > 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'.
I know that. I'm just pointing out that the OP can solve his *stated* problem of creating a *fixed* number of variables with *known* names without any magic. I went on to discuss the case of an unknown number of unknown names, and suggested that the OP not do that, because it is much less useful than people think. > 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. Of course. And even dynamically creating names: names don't exist until they are created at runtime. Name deletion is also possible. But what isn't normally done is "dynamic variable creation" in the sense of creating arbitrary variables (names bound to objects) based on names known only at runtime. That's extremely rare, and for good reason. -- Steven -- http://mail.python.org/mailman/listinfo/python-list