Multiple assignment?
Andy Jewell
andy at wild-flower.co.uk
Mon Apr 21 18:22:26 EDT 2003
On Monday 21 Apr 2003 10:55 am, Mehta, Anish wrote:
> Can someone tell me the what is being done here in this line of code.
>
> dir, file = os.path.split(input)
>
> Waiting for reply
>
> Thanks
>
> Anish MEHTA
1) Check the docs at python.org.
2) os.path.split cuts a file path into two parts, the directory bit, and the
file name bit, and returns it as a tuple.
3) Example of tuples in use:
>>> def swap(a,b): # This is NOT the best way to do this - example only
return b,a
>>> swap(1,2)
(2, 1)
>>> x=1
>>> y=2
>>> x,y=swap(x,y)
>>> x
2
>>> y
1
4) The final answer is that the statement:
dir, file = os.path.split(input)
'unpacks' the tuple returned by os.path.split() into the variables dir and
file.
5) 'file' is a bad choice for a variable name, as it is also a builtin class
for opening files (depending on your version of python).
More information about the Python-list
mailing list