create global variables?

Steve Holden steve at holdenweb.com
Mon Oct 30 22:35:05 CET 2006


Wojciech Muła wrote:
> Alistair King wrote:
> 
>>is there a simple way of creating global variables within a function?
> 
> 
> def foo(name):
> 	globals()[name] = "xxx"
> 	globals()[name + 'aa'] = "yyy"
> 	globals()[name + 'ab'] = "zzz"

Please note that this is a terrible programming practice. Anyone who 
really thinks they need to do this should be thinking hard about whether 
they are using Python correctly (though tere are occasionaly 
requirements where the feature can legitimately be used).

It would be *much* easier and *much* better programming practice to 
modify your code so the function returns a two-element tuple, which you 
then assign to two variables in an unpacking assignment.

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd          http://www.holdenweb.com
Skype: holdenweb       http://holdenweb.blogspot.com
Recent Ramblings     http://del.icio.us/steve.holden




More information about the Python-list mailing list