Question: Why does EXIT appear as the first line of my output?
Let me preface this by saying that I wrote this code while experimenting on solving a particular problem in a python script I am writing. So, yes, I am aware that this code may look rather ugly.
I cannot figure out why EXIT is shown first. I've even tried moving 'Quit' : 'EXIT' to the beginning of the dict (before 'Settings', but it still displays the same. I can't tell whether has something to do with the nature of iterkeys or dictionaries in general.
At first, my guess was that it might have something to do with the fact that mmenu['Quit'] is a string and mmenu['Settings'] is a dict, but then I noticed how the following is outputted in this order:
Rename: Enter a new name
Where?
Delete: Select Menu Item to Delete
when they were coded as:
'Rename': 'Rename: Enter a new name',
'Delete': 'Delete: Select Menu Item to Delete',
'Move_Item': 'Where?'
but what I can't figure out is why that matters.
Any insight is appreciated :)
#!/usr/bin/python
from inspect import isfunction
def create():
print "create function: you've just created a menu"
pass
mmenu = {
'Settings':
{
'Edit':
{
'Create': create,
'Modify':
{
'Rename': 'Rename: Enter a new name',
'Delete': 'Delete: Select Menu Item to Delete',
'Move_Item': 'Where?'
}
},
'Default_Settings': 'Default Settings'},
'Quit' : 'EXIT'
}
nmenu = mmenu
omenu = {}
def dictloop(dmenu):
global omenu
nmenu = dmenu
for key in nmenu.iterkeys():
if isfunction(nmenu[key]):
nmenu[key]()
if type(nmenu[key]) is str:
print nmenu[key]
if type(nmenu[key]) is dict:
omenu = nmenu[key]
dictloop(omenu)
dictloop(nmenu)
OUTPUT:
EXIT
create function: you've just created a menu
Rename: Enter a new name
Where?
Delete: Select Menu Item to Delete
Default Settings