problem with variable and function

Alex Hall mehgcap at gmail.com
Sun Mar 14 15:12:35 EDT 2010


Below is pasted the function which is looking for the "funcs"
dictionary, as well as the dictionary. They appear in my py file in
this order, yet I get an error in nextMode() that "global name 'funcs'
is not defined". Oddly, the keys dictionary works fine; it is defined
above the nextMode function.

def nextMode():
 global HOTKEYS
 global HOTKEY_ACTIONS
 global mode
 global modes
 global modeNum
 global modeNames
 global funcs
 #mode=mode+1
 #check to make sure the newly selected mode is enabled
 tmp=0
 while(tmp<modeNum):
  mode=(mode+1)%modeNum
  if(sys.modules[modeNames[mode]].enabled=='True'):
   break #break on the first enabled mode we find
  #end if
  tmp+=1
 #end while
 HOTKEYS=keys[mode]
 HOTKEY_ACTIONS=funcs[mode]
 registerHotkeys()
 speak("Now in "+str(modes[mode])+" mode.")
#end def

#we now have the default mode to be used, but what if it is disabled?
if(sys.modules[modeNames[mode]].enabled=='False'):
 nextMode()
#end if

funcs=[]
#this dict MUST be defined after all the functions it uses have been
#ARM function dictionary
funcs.append({
  1 : exitProgram,
  2 : arm.sayLoad1,
  3 : arm.sayLoad2,
  4 : arm.sayLoad3,
  5 : arm.sayLoad4,
  6 : arm.sayProcAvg,
  7 : arm.sayUsedRam,
  8 : arm.sayDisk1Info,
  9 : arm.sayDisk2Info,
  10 : nextMode,
  11: clipboard.toClipboard
})

#weather function dictionary
funcs.append({
  1 : exitProgram,
  2 : weather.getCurrent,
  3 : weather.getToday,
  4 : weather.getTomorrow,
  5 : weather.getTwoDays,
  6 : weather.getThreeDays,
  7 : weather.switchLocation,
  8 : arm.sayDisk1Info,
  9 : arm.sayDisk2Info,
  10 : nextMode,
  11 : clipboard.toClipboard
})
funcs.append({
  1 : exitProgram,
  2 : network.speed,
  3 : arm.sayLoad2,
  4 : arm.sayLoad3,
  5 : arm.sayLoad4,
  6 : arm.sayProcAvg,
  7 : arm.sayUsedRam,
  8 : arm.sayDisk1Info,
  9 : arm.sayDisk2Info,
  10 : nextMode,
  11 : clipboard.toClipboard
})


HOTKEY_ACTIONS=funcs[mode]


On 3/14/10, Chris Rebert <clp2 at rebertia.com> wrote:
> On Sun, Mar 14, 2010 at 10:26 AM, Alex Hall <mehgcap at gmail.com> wrote:
>> Hi all,
>> I have a file with a dictionary and a function. The dictionary holds
>> the name of the function, and the function references the dictionary.
>> If I put the dictionary first, the function is happy but the
>> dictionary says the function is not defined. If I switch the two and
>> put the function first, the function says the dictionary does not
>> exist. Does anyone have an idea as to how I can make both of them
>> happy?
> <snip>
>> Reverse it, though:
>>
>> def myFunc():
>>  myOtherVar=myVar
>>
>> myVar={
>>  1:myFunc
>> }
>>
>> and the function myFunc does not see the dictionary.
>
> Please be more specific in what you mean by it not "seeing" the
> dictionary, because the "reversed" approach *should* work:
>
> $ python
> Python 2.6.4 (r264:75706, Feb 25 2010, 01:21:39)
> [GCC 4.2.1 (Apple Inc. build 5646) (dot 1)] on darwin
> Type "help", "copyright", "credits" or "license" for more information.
>>>> def foo():
> ...     bar = baz
> ...     print bar
> ...
>>>> baz = {1:foo}
>>>> foo()
> {1: <function foo at 0x37b870>}
>
> Cheers,
> Chris
> --
> http://blog.rebertia.com
>


-- 
Have a great day,
Alex (msg sent from GMail website)
mehgcap at gmail.com; http://www.facebook.com/mehgcap



More information about the Python-list mailing list