args v. *args passed to: os.path.join()

Pierre Fortin pfortin at
Sat Sep 18 19:26:43 CEST 2004

This quest for understanding started very innocently... 

A simple error on my part, passing on args as "args" instead of "*args" to
os.path.join() led me to wonder why an error wasn't raised...

def foo(*args):
    return os.path.join(*args)

foo('a','b')    # returns 'a/b'

With: return os.path.join(args)

foo('a','b')    # returns ('a', 'b') (unchanged -- no error)

Replacing the 'return's in the function with each print below in turn
produced the results in the comments:

# without '*'
    print args                   # ('a', 'b')
    print (args)                 # ('a', 'b')
    print "/".join(args)         # a/b
    print string.join(args,"/")  # a/b
    print os.path.join(args)     # ('a', 'b')

# with '*'
    print *args                  # syntax error at *
    print (*args)                # syntax error at *
    print "/".join(*args)        # TypeError
    print string.join(*args,"/") # syntax error at 2nd (")
    print os.path.join(*args)    # a/b

The results in matrix form (numbers refer to order of print statements):

        args           *args
    1   ('a', 'b')     syntax error
    2   ('a', 'b')     syntax error
    3   a/b            TypeError
    4   a/b            syntax error 
    5   ('a', 'b')     a/b

os.path.join() is the only one in this list which doesn't return an error.
 Also, os.path.join()'s results are reversed relative to the others by
requiring *args...

My question is:  other than discovering this anomaly (and wrong result) at
runtime, is there a simple rule for when to pass on '*args' v. 'args' that
I've somehow missed...?


More information about the Python-list mailing list