*tuple vs tuple example printos.path.join(os.path.dirname(os.tmpnam()), *("a", "b", "c"))
fredrik at pythonware.com
Tue Dec 13 23:25:17 CET 2005
> I have been trying to find documentation on the behavior
the behaviour of what ?
> Can anyone tell me why the first example works and the second doesn't
> and where I can read about it in the language reference?
the os.path.join documentation (in the library reference) says
join(path1[, path2[, ...]])
Joins one or more path components intelligently. /.../ The return
value is the concatenation of path1, and optionally path2, etc.,
with exactly one directory separator (os.sep) inserted between
that is, the syntax is
path = os.path.join("part1", "part2", "part3")
your first example
> print os.path.join(os.path.dirname(os.tmpnam()),*("a","b","c"))
is equivalent to
print os.path.join(os.path.dirname(os.tmpnam()), "a", "b", "c")
which matches the description in the library reference.
the "*" notation is described, among other places, in the "calls"
section of the language reference:
"If the syntax "*expression" appears in the function call,
"expression" must evaluate to a sequence. Elements from
this sequence are treated as if they were additional
you second example
> print os.path.join(os.path.dirname(os.tmpnam()),("a","b","c")) #
passes in a tuple as path2, which doesn't match the library reference.
since Python expects you to pass in a string, you get an exception when
you pass in something else.
More information about the Python-list