Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > *tuple vs tuple example print os.path.join(os.path.dirname(os.tmpnam()),*("a","b ","c"))

Reply
Thread Tools

*tuple vs tuple example print os.path.join(os.path.dirname(os.tmpnam()),*("a","b ","c"))

 
 
Steve
Guest
Posts: n/a
 
      12-13-2005
I have been trying to find documentation on the behavior
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?
Steve

print os.path.join(os.path.dirname(os.tmpnam()),*("a","b ","c"))
#works
OUTPUT:/var/tmp/a/b/c
and

print os.path.join(os.path.dirname(os.tmpnam()),("a","b" ,"c")) #
doesn't
OUTPUT:Traceback (most recent call last):
File "<stdin>", line 1, in ?
File
"/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/posixpath.py",
line 60, in join
if b.startswith('/'):
AttributeError: 'tuple' object has no attribute 'startswith'

 
Reply With Quote
 
 
 
 
Fredrik Lundh
Guest
Posts: n/a
 
      12-13-2005
Steve wrote:

> 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
components /.../

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:

http://docs.python.org/ref/calls.html

"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
positional arguments"


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.

</F>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: TypeError: unsupported operand type(s) for -: 'tuple' and 'tuple' Jay Parlar Python 0 03-17-2006 02:58 AM
Why tuple with one item is no tuple Gregor Horvath Python 37 03-30-2005 06:58 AM
Easily convert unicode tuple to python string tuple??? Michal Mikolajczyk Python 1 04-20-2004 08:37 PM
Re: Easily convert unicode tuple to python string tuple??? Jeff Epler Python 0 04-20-2004 03:36 PM
Re: Easily convert unicode tuple to python string tuple??? Bill Scherer Python 0 04-20-2004 03:34 PM



Advertisments