Is vars() the most useless Python built-in ever?

Peter Otten __peter__ at web.de
Wed Dec 2 02:51:56 EST 2015


Steven D'Aprano wrote:

> and your code will break. Better and safer is:
> 
> 
> def main():  # parse the args and call whatever function was selected
>     args = parser.parse_args(sys.argv[1:])
>     try:
>         func = args.func
>     except AttributeError as err:
>         parser.print_help()
>     else:
>         func(vars(args))

I don't think this is any better. You are reponding on a programming error 
in the script as if the user had provided erroneous input. Very confusing.
Just

    args.func(vars(args))

will produce a traceback if the programmer made an error and a help message 
if the user provides arguments that are not recognized. 

That's how it should be.




More information about the Python-list mailing list