dictionary that have functions with arguments

Tim Williams (gmail) tdwdotnet at gmail.com
Wed Nov 2 11:39:28 EST 2005


On 1 Nov 2005 20:02:41 -0800, s99999999s2003 at yahoo.com
<s99999999s2003 at yahoo.com> wrote:
> hi
> i have a dictionary defined as
>
> execfunc = { 'key1' : func1 }

##################

def __HELLO(x=' '):
    print 'HELLO',x

def __BYE(x=' '):
    print 'BYE',x

def __PRINT(x=None, y=None):
    print 'PRINT',x,y

cmds = { 'HELLO' : __HELLO,
           'BYE' : __BYE,
           'PRINT' : __PRINT,
           }

a = 'HELLO JOE'
b = a.split()

if cmds.has_key(b[0]):
    cmds[b[0]](b[1])
# -> HELLO JOE

cmds[b[0]]()
# -> HELLO

cmds['BYE']('TOM')
# -> BYE TOM

cmds['PRINT']( 'TOM','JOE' )
# -> PRINT TOM JOE

cmds['PRINT']
# -> *No output - No exception

#####################

Inside  a class use

cmds = { 'HELLO' : self.__HELLO,  # etc

def __HELLO(self, x=' '): #etc


HTH :)



More information about the Python-list mailing list