From: Scott D. D. <Sco...@Ac...> - 2006-03-28 01:01:45
|
John Brawley wrote: > I realize this is not the list for this question, but some of you might > know, and I'm not currently on the Python list.... > > Can anyone tell me how Python implements C++ in the case of _tuples_? This question makes no sense. > I'm assuming Python is built somewhat on C (or C++), and in C++ you can't > return more than one item from a function, but Python allows one to return > as many (up to 10?) variables as one wants (this is one reason Python is so > "powerful"; returning three variables at once makes it a 'natural' for 3D > space work with lots and lots of x,y,z dealings). Python functions only ever return a single value, but that value might be a tuple of values. There is no limit of 10 (or so) values in that tuple. Consider: def powers(a): return (1, a, a**2, a**3, a**4, a**5, a**6, a**7, a**8, a**9, a**10, a**11, a**12, a**13) a,b,c,d,e,f,g,h,i,j,k,l,m,n = powers(1.2) Or even: def pows(a, number): return [a ** n for n in range(number)] a,b,c,d = pows(1.2, 4) a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z = pows(1.1, 26) Modern C (and C++) similarly allow you to return a struct. Take a python function that you think produces multiple results, and simply print the result of that function. It is a single item which is "dis-assembled" in the python assignment. For example, you can do the following: (a, b), (c, d, e) = [(1, 3), [1.1, 2.3, 'f']] -- -- Scott David Daniels Sco...@Ac... |