Function lookup using a directory.

Ype Kingma ykingma at accessforall.nl
Mon Jun 18 20:47:05 CEST 2001


Andy,

In addition to the earlier replies:

Your run() method seems to be checking for
presence of arguments, so you might
consider defining it with an extra '*' as:

class Help0:
    def run(self, *which):
        if len(which) == 1:
            print 'Help no args'
        else:
            print 'Help args'

This will give you a tuple with the passed arguments,
in your case an empty tuple.

Have fun,
Ype


andy.pevy at nokia.com wrote:
> 
> Hi Guys
> 
>         I am having trouble with the directory data type. What I want to
> do is to set up a directory that I can use to parse a command entered by
> the user, and have a directory return the name of a class to handle the
> command.  Like this :-
> 
> command_table = {'0':    'Help0',
>             '1':    'Help1',
>             '2':    'Help2',
>             '3':    'Help3' }
> 
> class Help0:
> 
>     def run(self,which):
>         if len(which) == 1:
>             print 'Help no args'
>         else:
>             print 'Help args'
> 
[snip]
> 
> if __name__ == '__main__':
> 
>     command = user_input()
> 
>     try:
>         h = command_table[command[0]]
>     except:
>         print command[0]+' not found'
> 
>     h.run()     # Call the run method of the selected class.
> 


-- 
email at xs4all.nl



More information about the Python-list mailing list