A new to Python question

D

David

Hi I'm trying to teach myself python and so far to good, but I'm having
a bit of trouble getting a function to work the way I think it should
work. Right now I'm taking a simple program I wrote in Fortran and
trying to do it in Python. I got it to work, but now I'm trying to
modularize it. My fortran program uses a subroutine and I was trying
to do the same thing in Python. But I'm still new so I'm having
trouble understanding what I'm doing wrong. Here is my code:

#! /usr/bin/python
#This program takes two vectors and multiplies them
#against a 10X10 array. It also then gives the dot product,
#sum, and max value of the array.

import Numeric
def abc(array1,array2,dotprod,sum,maxvalue):
"""Takes two arrays and performs predetermined calculations,
Then returns the solutions back as the same array, along
with the dot product, sum, and max value of the array."""
#TODO: Get this damn thing working!!
print "Array 1 in:",array1 #Debug
data = Numeric.zeros((10,10))
for i in range(10):
for j in range(10):
data[i,j] = (i+1)+(j+1)

e = Numeric.matrixmultiply(data, array1)
g = Numeric.matrixmultiply(Numeric.transpose(data),array2)
array1 = e
array2 = g
dotprod = Numeric.dot(array1,array2)
sum = Numeric.sum(array1)
maxvalue = array2[Numeric.argmax(array2)]
print "Array 1 out:",array1 #Debug

return array1,array2,dotprod,sum,maxvalue #<<-- Not working as I
thought it would.

x = Numeric.arange(1,11)
y = Numeric.arange(1,11)*2
dotp,sumx,maxv = 0,0,0 #Is this the only way to declare a variable?

print 'Array X:',x
print 'Array Y:',y
abc(x,y,dotp,sumx,maxv)
print 'Calling function abc'
print 'Array X:',x
print 'Array Y:',y
print 'Dot Product of X and Y:',dotp
print 'Sum of array X:',sumx
print 'Max value of array Y:',maxv

If you run it the data gets passed to the function just fine and it
finds the right numbers. Its just getting it to pass them back thats
not working. I put some print statements inside the function just to
see how the data gets passed. So any ideas that would help me? If you
want to see the fortran code just email me.

David
 
P

Philippe C. Martin

Hi,

You're thinking you're passing the arguments as reference (look at mutable
vs non-mutable)

Your function returns the values in a tupple (x,y,...); you need to fetch
the values from that tupple

Regards,

Philippe
 
M

M.E.Farmer

David said:
Hi I'm trying to teach myself python and so far to good, but I'm having
a bit of trouble getting a function to work the way I think it should
work. Right now I'm taking a simple program I wrote in Fortran and
trying to do it in Python. I got it to work, but now I'm trying to
modularize it. My fortran program uses a subroutine and I was trying
to do the same thing in Python. But I'm still new so I'm having
trouble understanding what I'm doing wrong. Here is my code:

#! /usr/bin/python
#This program takes two vectors and multiplies them
#against a 10X10 array. It also then gives the dot product,
#sum, and max value of the array.

import Numeric
def abc(array1,array2,dotprod,sum,maxvalue):
"""Takes two arrays and performs predetermined calculations,
Then returns the solutions back as the same array, along
with the dot product, sum, and max value of the array."""
#TODO: Get this damn thing working!!
print "Array 1 in:",array1 #Debug
data = Numeric.zeros((10,10))
for i in range(10):
for j in range(10):
data[i,j] = (i+1)+(j+1)

e = Numeric.matrixmultiply(data, array1)
g = Numeric.matrixmultiply(Numeric.transpose(data),array2)
array1 = e
array2 = g
dotprod = Numeric.dot(array1,array2)
sum = Numeric.sum(array1)
maxvalue = array2[Numeric.argmax(array2)]
print "Array 1 out:",array1 #Debug

return array1,array2,dotprod,sum,maxvalue #<<-- Not working as I
thought it would.

x = Numeric.arange(1,11)
y = Numeric.arange(1,11)*2
dotp,sumx,maxv = 0,0,0 #Is this the only way to declare a variable?

print 'Array X:',x
print 'Array Y:',y
abc(x,y,dotp,sumx,maxv)
print 'Calling function abc'
print 'Array X:',x
print 'Array Y:',y
print 'Dot Product of X and Y:',dotp
print 'Sum of array X:',sumx
print 'Max value of array Y:',maxv

If you run it the data gets passed to the function just fine and it
finds the right numbers. Its just getting it to pass them back thats
not working. I put some print statements inside the function just to
see how the data gets passed. So any ideas that would help me? If you
want to see the fortran code just email me.

David

Hello David,
welcome to Python!
Python always returns 'something' from a function or method if nothing
is specified or there is no return you get an implicit None.
You have created a function that accepts a few arguments and processes
them then returns there value.
The problem is that you **have not** used the return values ;)
example:

def funk(first,second,third):
return first,second,third

This can be like this:
print funk("theses three args can be anything",2,"three")

If you need to use the return values you need to assign them to a
"name" so you have a handle on them.
Variables are don't exist in Python there are only objects and names(
hence namespaces ), but not everyone is so strict and you still see
mention of 'variables' when they mean 'names'.
##dotp,sumx,maxv = 0,0,0 # not needed here
print 'Array X:',x
print 'Array Y:',y
## Notice we assign the return values to names so we can access it later
arr1,arr2,dotp,sumx,maxv = abc(x,y,0,0,0)
print 'Calling function abc'
print 'Array X:',arr1
print 'Array Y:',arr2
print 'Dot Product of X and Y:',dotp
print 'Sum of array X:',sumx
print 'Max value of array Y:',maxv

Or you could pack all return values into a tuple and access it thru
slices.
##dotp,sumx,maxv = 0,0,0 # not needed here
print 'Array X:',x
print 'Array Y:',y
abcinfo = abc(x,y,0,0,0)
print 'Calling function abc'
print 'Array X:',abcinfo[0]
print 'Array Y:',abcinfo[1]
print 'Dot Product of X and Y:',abcinfo[2]
print 'Sum of array X:',abcinfo[3]
print 'Max value of array Y:',abcinfo[4]

Or you could use a dictionary, or etc...
The possibilities are endless.
Be sure to study up on namespaces, it will ease your Python woes.
Namespaces are the most fundamental part of Python that new users don't
understand. Namespace mastery will take you far.
Just remember there are only two scopes local and global ;)
http://docs.python.org
hth,
M.E.Farmer
 
M

M.E.Farmer

(x,y,dotp,sumx,maxv) = abc(x,y,dotp,sumx,maxv)
This will only create a tuple in memory that has no name to reference
it by!
How would you access the returned value?
If you want a tuple you need to assign all the return vales to a single
name.
mytup = abc(x,y,dotp,sumx,maxv)
M.E.Farmer
 
S

Steven Bethard

M.E.Farmer said:
(x,y,dotp,sumx,maxv) = abc(x,y,dotp,sumx,maxv)
This will only create a tuple in memory that has no name to reference
it by!

Huh? This binds the names "x", "y", "dotp", "sumx" and "maxv" to the
values returned by abc:

py> def abc(*args):
.... return args
....
py> (x,y,dotp,sumx,maxv) = abc(2,3,5,7,11)
py> x
2
py> y
3
py> dotp
5
py> sumx
7
py> maxv
11

Of course, the parentheses around x,y,dotp,sumx,maxv are unnecessary.

STeVe
 
D

David

Thank you very much. Tulpes are really neat now that I've looked at
them. So after just fixing it, I changed it up and I would like to
show it off now.

#! /usr/bin/python
#This program takes two vectors and multiplies them
#against a 10X10 array. It also then gives the dot product,
#sum, and max value of the array.

import Numeric
def abc(package):
"""Takes two arrays and performs predetermined calculations,
Then returns the solutions back as the same array, along
with the dot product, sum, and max value of the array.

Data slots:
0 - First Array
1 - Second Array
2 - Dot product
3 - Sum
4 - Max Value"""
f = Numeric.zeros((10,10))
for i in range(10):
for j in range(10):
f[i,j] = (i+1)+(j+1)

e = Numeric.matrixmultiply(f, package[0])
g = Numeric.matrixmultiply(Numeric.transpose(f),package[1])
package[0] = e
package[1] = g
package[2] = Numeric.dot(package[0],package[1])
package[3] = Numeric.sum(package[0])
package[4] = package[1][Numeric.argmax(package[1])]
return package

data = [Numeric.arange(1,11),Numeric.arange(1,11)*2,0,0,0]
#data = [Array X, Array Y, Dot product, Sum, Max Value]

print 'Array X:',data[0]
print 'Array Y:',data[1]

data = abc(data)

print 'Calling function abc'
print 'Array X:',data[0]
print 'Array Y:',data[1]
print 'Dot Product of X and Y:',data[2]
print 'Sum of array X:',data[3]
print 'Max value of array Y:',data[4]

I think its just wonderful now, but if you got any other suggestions,
Please do tell. Thanks everyone.

David
 
B

Bernd Nawothnig

On 2005-05-14, M.E.Farmer wrote:
(x,y,dotp,sumx,maxv) = abc(x,y,dotp,sumx,maxv)
This will only create a tuple in memory that has no name to reference
it by!

Maybe. But it does not seem to hurt. And I am not sure the tupel _is_
really created in that situation.
How would you access the returned value?
If you want a tuple you need to assign all the return vales to a single
name.

We do not want the tuple.


Python 2.3.5 (#1, Apr 28 2005, 12:14:04)
[GCC 3.4.3-20050110 (Gentoo Linux 3.4.3.20050110-r2,
ssp-3.4.3.20050110-0, pie-
on linux2
Type "help", "copyright", "credits" or "license" for more information.
works.

Of course, you can omit the ():
1 2 3

That makes no difference.




Bernd
 
R

Robert Kern

David said:
Thank you very much. Tulpes are really neat now that I've looked at
them. So after just fixing it, I changed it up and I would like to
show it off now.

#! /usr/bin/python
#This program takes two vectors and multiplies them
#against a 10X10 array. It also then gives the dot product,
#sum, and max value of the array.

import Numeric
def abc(package):
"""Takes two arrays and performs predetermined calculations,
Then returns the solutions back as the same array, along
with the dot product, sum, and max value of the array.

Data slots:
0 - First Array
1 - Second Array
2 - Dot product
3 - Sum
4 - Max Value"""
f = Numeric.zeros((10,10))
for i in range(10):
for j in range(10):
f[i,j] = (i+1)+(j+1)

e = Numeric.matrixmultiply(f, package[0])
g = Numeric.matrixmultiply(Numeric.transpose(f),package[1])
package[0] = e
package[1] = g
package[2] = Numeric.dot(package[0],package[1])
package[3] = Numeric.sum(package[0])
package[4] = package[1][Numeric.argmax(package[1])]
return package

data = [Numeric.arange(1,11),Numeric.arange(1,11)*2,0,0,0]
#data = [Array X, Array Y, Dot product, Sum, Max Value]

print 'Array X:',data[0]
print 'Array Y:',data[1]

data = abc(data)

print 'Calling function abc'
print 'Array X:',data[0]
print 'Array Y:',data[1]
print 'Dot Product of X and Y:',data[2]
print 'Sum of array X:',data[3]
print 'Max value of array Y:',data[4]

I think its just wonderful now, but if you got any other suggestions,
Please do tell. Thanks everyone.

It's still not very Pythonic. Don't bother putting outputs in the
argument list. There are some good use cases for doing so, but until
you've been writing Python code for a while and the Pythonic idioms come
naturally to you, you should probably resist the urge. Python is not
Fortran.

def abc(x, y):
...
e = Numeric.matrixmultiply(f, x)
g = Numeric.matrixmultiply(Numeric.transpose(f), y)
dotp = Numeric.dot(x, y)
sumx = Numeric.sum(x)
maxy = Numeric.maximum.reduce(y)
return e, g, dotp, sumx, maxy

x = Numeric.arange(1,11)
y = x*2

x, y, dotp, sumx, maxy = abc(x, y)

--
Robert Kern
(e-mail address removed)

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
 
M

M.E.Farmer

I said exactly what I meant, the parentheses around the values creates
a tuple that you have no reference to! It also has a side effect of
binding the names inside the tuple to a value and placing them in the
local namespace( implicit tuple unpacking ). It might be the "same" as
no parens but it isn't very clear. If you want a tuple make it
explicit, if you want individual names make it explicit..... return q,w,e,r
....['__builtins__', '__doc__', '__name__', 'a', 'b', 'c', 'd', 'f',
'shell']['__builtins__', '__doc__', '__name__', 'a', 'b', 'c', 'd', 'f',
'shell']['__builtins__', '__doc__', '__name__', 'f', 'shell', 'tup']
Now there is.
Steve since you are closer to expert than novice you understand the
difference.
I feel this can be confusing to newbies, maybe you disagree.
M.E.Farmer
 
M

M.E.Farmer

I said exactly what I meant, the parantheses around the values creates
a tuple that you have no reference to! It also has a side effect of
binding the names inside the tuple to a value and placing them in the
local namespace. It might be the "same" but it isn't very clear. If you
want a tuple make it explicit, if you want individual names make it
explicit..... return q,w,e,r
....['__builtins__', '__doc__', '__name__', 'a', 'b', 'c', 'd', 'f',
'shell']['__builtins__', '__doc__', '__name__', 'a', 'b', 'c', 'd', 'f',
'shell']['__builtins__', '__doc__', '__name__', 'f', 'shell', 'tup']
Now there is.
Steve since you are closer to expert than novice you understand the
difference.
I feel this can be confusing to newbies, maybe you disagree.
M.E.Farmer
 
B

Bengt Richter

Maybe. But it does not seem to hurt. And I am not sure the tupel _is_
really created in that situation.
One way to get insight into what a statement does is to disassemble its code, e.g.,
1 0 LOAD_NAME 0 (abc)
3 LOAD_NAME 1 (x)
6 LOAD_NAME 2 (y)
9 LOAD_NAME 3 (dotp)
12 LOAD_NAME 4 (sumx)
15 LOAD_NAME 5 (maxv)
18 CALL_FUNCTION 5
21 UNPACK_SEQUENCE 5
24 STORE_NAME 1 (x)
27 STORE_NAME 2 (y)
30 STORE_NAME 3 (dotp)
33 STORE_NAME 4 (sumx)
36 STORE_NAME 5 (maxv)
39 LOAD_CONST 0 (None)
42 RETURN_VALUE

In the context of a function or module scope, those loads and stores will change
to optimized byte code ops like LOAD_FAST and STORE_FAST etc, but you can see what's
happening above. I.e., on return from the function call, the returned sequence (we know it's
a tuple in your example, but it could be some other sequence thing) is upacked and its elements
are bound to the individual names, just as individual "assignments" would do.

Regards,
Bengt Richter
 
S

Steven Bethard

M.E.Farmer said:
I said exactly what I meant, the parentheses around the values creates
a tuple that you have no reference to! It also has a side effect of
binding the names inside the tuple to a value and placing them in the
local namespace( implicit tuple unpacking ). It might be the "same" as
no parens but it isn't very clear. If you want a tuple make it
explicit, if you want individual names make it explicit.

It actually is the same, and I don't think implicit or explicit is the
difference you should be citing here. The parentheses are fully
optional -- they don't change the semantics at all:

py> t = (4, 5)
py> a = t
py> a is t
True
py> a = (b, c) = t
py> a is t
True
py> a = b, c = t
py> a is t
True

In all cases, "a" still gets assigned the tuple (4, 5). Whether or not
you put the parentheses around "b, c" is fully a matter of style.

I don't know the implementation enough to know whether or not a tuple is
actually created when "b" and "c" are bound to their values, but I'd be
willing to bet that whatever happens to "(b, c)" is exactly the same as
what happens to "b, c".

STeVe
 
S

Steven Bethard

Steven said:
I don't know the implementation enough to know whether or not a tuple is
actually created when "b" and "c" are bound to their values, but I'd be
willing to bet that whatever happens to "(b, c)" is exactly the same as
what happens to "b, c".

Some corroborating evidence:

py> def f(t):
.... (b, c) = t
....
py> def g(t):
.... b, c = t
....
py> import dis
py> dis.dis(f)
2 0 LOAD_FAST 0 (t)
3 UNPACK_SEQUENCE 2
6 STORE_FAST 2 (b)
9 STORE_FAST 1 (c)
12 LOAD_CONST 0 (None)
15 RETURN_VALUE
py> dis.dis(g)
2 0 LOAD_FAST 0 (t)
3 UNPACK_SEQUENCE 2
6 STORE_FAST 2 (b)
9 STORE_FAST 1 (c)
12 LOAD_CONST 0 (None)
15 RETURN_VALUE

They're both an UNPACK_SEQUENCE byte-code.

STeVe
 
B

Bengt Richter

On 14 May 2005 10:44:30 -0700 said:
Be sure to study up on namespaces, it will ease your Python woes.
Namespaces are the most fundamental part of Python that new users don't
understand. Namespace mastery will take you far.
Just remember there are only two scopes local and global ;)
And don't jump to conclusions when you read a line like the last above ;-)
I.e., there can be many different local and global scopes, not just one of each,
and since Python is a very dynamic language, existence of things can vary with
execution time and logic, so those scopes don't all have to exist at the same time,
or at all, nor persist until the program runs to its end. Also, ISTM "local" is probably
being stretched a little above to include nested scopes, which are a bit different
from either plain local or global in how you can access them.

I would second the advice to explore namespaces, and particularly attribute name spaces
such as are defined by classes. Attribute access and descriptors conspire to enable much
of the magic you can cook up with Python, as well as stuff that happens automatically.

In short, a whole unopened deluxe set of programming Legos is at your elbow, besides the
pieces you have encountered so far. Have fun ;-)

Regards,
Bengt Richter
 
M

M.E.Farmer

It actually is the same, and I don't think implicit or explicit is the
difference you should be citing here.
Why not it is relevant and true that Python is doing an implicit
unpacking of the values in that unnamed tuple.
Quoting myself """It might be the "same" as
no parens but it isn't very clear. If you want a tuple make it
explicit, if you want individual names make it explicit."""
So as you say, maybe it is is fully a matter of style and apparently I
don't like that style!
They're both an UNPACK_SEQUENCE byte-code.
I have dissed it myself ;)
My argument, if you can call it that, is that it is not clear that
Python is going to do a tuple unpacking here or not !
You had to dis it to find out and you are not a newbie.
# extra fluff from pyshellabc(x,y,dotp,sumx,maxv)','','exec'))
0 SET_LINENO 0

3 SET_LINENO 1
6 LOAD_NAME 0 (abc)
9 LOAD_NAME 1 (x)
12 LOAD_NAME 2 (y)
15 LOAD_NAME 3 (dotp)
18 LOAD_NAME 4 (sumx)
21 LOAD_NAME 5 (maxv)
24 CALL_FUNCTION 5
27 UNPACK_SEQUENCE 5
30 STORE_NAME 1 (x)
33 STORE_NAME 2 (y)
36 STORE_NAME 3 (dotp)
39 STORE_NAME 4 (sumx)
42 STORE_NAME 5 (maxv)
45 LOAD_CONST 0 (None)
48 RETURN_VALUEabc(x,y,dotp,sumx,maxv)','','exec'))
0 SET_LINENO 0

3 SET_LINENO 1
6 LOAD_NAME 0 (abc)
9 LOAD_NAME 1 (x)
12 LOAD_NAME 2 (y)
15 LOAD_NAME 3 (dotp)
18 LOAD_NAME 4 (sumx)
21 LOAD_NAME 5 (maxv)
24 CALL_FUNCTION 5
27 UNPACK_SEQUENCE 5
30 STORE_NAME 1 (x)
33 STORE_NAME 2 (y)
36 STORE_NAME 3 (dotp)
39 STORE_NAME 4 (sumx)
42 STORE_NAME 5 (maxv)
45 LOAD_CONST 0 (None)
48 RETURN_VALUE 0 SET_LINENO 0

3 SET_LINENO 1
6 LOAD_NAME 0 (abc)
9 LOAD_NAME 1 (x)
12 LOAD_NAME 2 (y)
15 LOAD_NAME 3 (dotp)
18 LOAD_NAME 4 (sumx)
21 LOAD_NAME 5 (maxv)
24 CALL_FUNCTION 5
27 STORE_NAME 6 (tup)
30 LOAD_CONST 0 (None)
33 RETURN_VALUEYou may notice that the act of assigning the return values to a unnamed
tuple and a name have different semantics and that was the real point.
The problem is why should this be the same:

a,b,c=(1,2,3)

(a,b,c)=(1,2,3)

Seems weird, non-intuitive that a tuple unpacking and tuple creation
have the same bytecode.
So why is this done? What is the reason, I am sure there are a few ;)

p.s. I am leaving out for a while and will read/followup later if
needed. Thank you all for your time.
M.E.Farmer
 
B

Bengt Richter

I said exactly what I meant, the parentheses around the values creates
a tuple that you have no reference to! It also has a side effect of
I don't understand what you are saying, or meaning ;-)
BTW, the function call has nothing to do with what happens on the left hand side
of the assignment. It is only the result of the function call that counts, so we
could as well use a literal or other expression. This is a matter of unpacking
a sequence according to the left hand target. E.g.,
>>> iter([1,2,3])
>>> a,b,c = iter([1,2,3])
>>> d,e,f = [1,2,3]
>>> g,h,i = 'GHI'
>>> [a,b,c,d,e,f,g,h,i]
[1, 2, 3, 1, 2, 3, 'G', 'H', 'I']

Ok, now looking at the code for the assignment to a,b,c vs to (a,b,c):
>>> import dis
>>> dis.dis(compile('(a,b,c) = iter([1,2,3])','','exec'))
1 0 LOAD_NAME 0 (iter)
3 LOAD_CONST 0 (1)
6 LOAD_CONST 1 (2)
9 LOAD_CONST 2 (3)
12 BUILD_LIST 3
15 CALL_FUNCTION 1
18 UNPACK_SEQUENCE 3
21 STORE_NAME 1 (a)
24 STORE_NAME 2 (b)
27 STORE_NAME 3 (c)
30 LOAD_CONST 3 (None)
33 RETURN_VALUE
>>> dis.dis(compile('a,b,c = iter([1,2,3])','','exec'))
1 0 LOAD_NAME 0 (iter)
3 LOAD_CONST 0 (1)
6 LOAD_CONST 1 (2)
9 LOAD_CONST 2 (3)
12 BUILD_LIST 3
15 CALL_FUNCTION 1
18 UNPACK_SEQUENCE 3
21 STORE_NAME 1 (a)
24 STORE_NAME 2 (b)
27 STORE_NAME 3 (c)
30 LOAD_CONST 3 (None)
33 RETURN_VALUE

I don't see anything in the code about creating a tuple that you have no reference to.
To see the unpacking of "GHI" so as to get rid of the function call red herring:
1 0 LOAD_CONST 0 ('GHI')
3 UNPACK_SEQUENCE 3
6 STORE_NAME 0 (a)
9 STORE_NAME 1 (b)
12 STORE_NAME 2 (c)
15 LOAD_CONST 1 (None)
18 RETURN_VALUE 1 0 LOAD_CONST 0 ('GHI')
3 UNPACK_SEQUENCE 3
6 STORE_NAME 0 (a)
9 STORE_NAME 1 (b)
12 STORE_NAME 2 (c)
15 LOAD_CONST 1 (None)
18 RETURN_VALUE

ISTM the assignment part starts with UNPACK_SEQUENCE and all the code looks the same.
BTW, UIAM it is the commas that define tuples, so the outermost parens are really expression
parens more than tuple syntax. But note that you can have nested tuple structure as target,
and that does change the unpacking code (and what it expects to have available to unpack):
Traceback (most recent call last):
File said:
>>> (x,(y,z)) = 'X','YZ'
>>> x 'X'
>>> y 'Y'
>>> z 'Z'
>>> (x,(y,z)) = [1, [2,3]]
>>> x 1
>>> y 2
>>> z 3
>>> dis.dis(compile('(x,(y,z)) = 0','','exec'))
1 0 LOAD_CONST 0 (0)
3 UNPACK_SEQUENCE 2
6 STORE_NAME 0 (x)
9 UNPACK_SEQUENCE 2
12 STORE_NAME 1 (y)
15 STORE_NAME 2 (z)
18 LOAD_CONST 1 (None)
21 RETURN_VALUE

Obviously we can't unpack the zero, but the code doesn't know
that until it tries it, so we can look at what the left hand side code is
independent of the right hand side.

binding the names inside the tuple to a value and placing them in the
local namespace( implicit tuple unpacking ). It might be the "same" as
no parens but it isn't very clear. If you want a tuple make it
explicit, if you want individual names make it explicit.
OTOH, you might want a mix:
>>> a, tup, (b,c), d = ['A', ('a', 'tuple'), 'BC', 'Sandra']
>>> a 'A'
>>> tup ('a', 'tuple')
>>> b 'B'
>>> c 'C'
>>> d
'Sandra'

Or with gratuitous _outer_ parens:
>>> (a, tup, (b,c), d) = ['A', ('a', 'tuple'), 'BC', 'Sandra']
>>> a 'A'
>>> tup ('a', 'tuple')
>>> b 'B'
>>> c 'C'
>>> d
'Sandra'
>>> dis.dis(compile("a, tup, (b,c), d = ['A', ('a', 'tuple'), 'BC', 'Sandra']",'','exec'))
1 0 LOAD_CONST 0 ('A')
3 LOAD_CONST 6 (('a', 'tuple'))
6 LOAD_CONST 3 ('BC')
9 LOAD_CONST 4 ('Sandra')
12 BUILD_LIST 4
15 UNPACK_SEQUENCE 4
18 STORE_NAME 0 (a)
21 STORE_NAME 1 (tup)
24 UNPACK_SEQUENCE 2
27 STORE_NAME 2 (b)
30 STORE_NAME 3 (c)
33 STORE_NAME 4 (d)
36 LOAD_CONST 5 (None)
39 RETURN_VALUE

Regards,
Bengt Richter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top