which

Jean-Michel Pichavant jeanmichel at sequans.com
Fri Feb 5 16:28:25 CET 2010


mk wrote:
> Jean-Michel Pichavant wrote:
>>
>> What is worrying me the most in your code sample is that self.cmd can 
>> hold diferrent types (str, and something else). That is usually a bad 
>> thing to do (putting None aside).
>> However, my remark could be totally irrelevant of course, that 
>> depends on the context.
>
> That's a valid criticism - but I do not know how to handle this 
> otherwise really, because the program can be called with "cmd" to run, 
> or a script to run (or a directory to copy) and in those cases cmd is 
> None.
>
> I guess I could use
>
> if cmd:
>     self.cmd = ...
>
>
> But. Suppose that under some circumstances cmd is not string. What then?
>
> I know that isinstance is typically not recommended, but I don't see 
> better solution here.
>
>
> Regards,
> mk
>
>
If you can change your program interface, then do it, if not then you're 
right you don't have much choice as you are suffering from the program 
poor interface.
You can fix this problem by explicitly asking for the thing you want to 
do, instead of guessing by inspecting the argument nature.

myProg --help

usage : myProg command [args]
    command list:
       - cmd: execute the given <arg1> command line
       - exec: execute the given script file named <arg1>
       - copy: copy <arg1> to <arg2>

example:
 >myProg cmd "echo that's cool"
 >myProg exec /etc/init.d/myDaemon
 >myProg copy /tmp /tmp2

JM



More information about the Python-list mailing list