Ganesh Pal wrote: > Iam using the options.name directly for manipulations is this fine or do > I need to assign it to variable and then use it
> if options.object_type == 'LIN': > corrupt_inode() This is fine. You would only consider storing the value if you are going to use it very often (millions rather than thousands) in a time-critical loop. > I wanted to use dictionary to match the above if else behavior (we don't > have switch in python I guess ) and If else looks very untidy. > Is it possible to store the options.object_type as a key in the dictionary > and then based on the value entered in the command line invoke the > appropriate function > I tried creating a dictionary like this but Iam getting a wrong output > object_type_dictonary = { 'LIN' : corrupt_inode(), > 'INODE' : corrupt_lin(), > 'DATA' : corrupt_data(), > }; > and then ran # python corrupt.py -object_type= inode ( This prints all > the values for me) That's because you are storing the result of the function call when you should be storing the function itself: # build the dispatch table: object_type_dictionary = { "LIN": corrupt_lin, # no () here, we're storing the function "INODE": corrupt_inode, "DATA": corrupt_data } ... handler = object_type_dictionary[options.object_type] # look up the function handler() # call it The last two lines could also be merged into one object_type_dictionary[options.object_type]() but the first version may be clearer. -- https://mail.python.org/mailman/listinfo/python-list