Re: create global variables?
- From: Steve Holden <steve@xxxxxxxxxxxxx>
- Date: Mon, 30 Oct 2006 21:35:05 +0000
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
.
- References:
- create global variables?
- From: Alistair King
- Re: create global variables?
- From: Wojciech Muła
- create global variables?
- Prev by Date: Re: Python windows interactive.
- Next by Date: Re: Python windows interactive.
- Previous by thread: Re: create global variables?
- Next by thread: Re: create global variables?
- Index(es):