accesibility of the namespace

Mikael Olofsson mikael at isy.liu.se
Thu Mar 9 14:34:24 CET 2006


Petr Jakes wrote:
> Ooooops. My keyboard (fingers) was faster than my mind :(
> So....
> There is more than one "run-time changed variable" in the dictionary
> and not all strings in the dictionary are formatted using % operator.
> Example:
> lcd={
> 2:{2:(("Enter you choice"),("Your kredit= %3d" % (kredit)))},
> 4:{2:(("Your choice: %2s" % (keyboard)),("%-20s" % (actKeyboard)))}}
> 
> I do not know which variable(s) to use for the % operator in the time
> of the call of the value (% formatted string) from the dictionary.
> 
> This is also the reason why the variable names are stored in the
> dictionary with the strings.
> 
> Any other suggestions?

So, use something like

 >>> lcd={
	2:{2:("Enter you choice","Your kredit= %(kredit)3d")},
	4:{2:("Your choice: %(keyboard)2s" ,"%(actKeyboard)-20s")}}
 >>> kredit = 7
 >>> keyboard = 'aa'
 >>> actKeyboard = 7654
 >>> lcd[2][2][-1] % vars()
'Your kredit=   7'
 >>> lcd[2][2][0] % vars()
'Enter you choice'
 >>> lcd[4][2][0] % vars()
'Your choice: aa'
 >>> lcd[4][2][1] % vars()
'7654                '
 >>>

HTH
/MiO



More information about the Python-list mailing list