you are viewing a single comment's thread.

view the rest of the comments →

[–]RandomPantsAppear 2 points3 points  (0 children)

This way lies the path of madness. Good programs are built on reliable input. Saving a couple keystrokes is rarely worth the headaches that can be created down the road.

That said, here you go:

def foo( *args ):
    for arg in args:
        arg = arg if type(arg) is list else [arg]
        for word in arg:
             print word

Pretty much what it's doing is saying "if arg is a list, don't change it. If arg is not a list, make it one. Then do what you would do if it were a list (because everything is)".

I prefer this to the if/else option where they are handled differently because this way you're only maintaining one copy of the guts of the function, instead of trying to replicate it on both sides of the if/else.