getting results into one variable

bruno at modulix onurb at xiludom.gro
Thu Nov 10 07:50:27 EST 2005


s99999999s2003 at yahoo.com wrote:
> hi
(snip)
> 
> in python, can we do something like
> 
> a = db.execute(stmt) and then expand variable 'a'
> instead of doing
> (a,b) =  db.execute(stmt) for return of 2
> (a,b,c) = for return of 3
> (a,b,c,d) for return of 4


Did you try ?-) Took me about 30'':

>>> def fun(): return 1,2,3
...
>>> a = fun()
>>> a
(1, 2, 3)
>>> def fun2(): return 1,2,3,4,8,9
...
>>> b = fun2()
>>> b
(1, 2, 3, 4, 8, 9)
>>>

It of course works since a function *always* returns a *single* value.
Now this value *can* be a sequence, and this sequence *can* be unpacked
- directly at the return of the function, or latter:

>>> a = fun()
>>> a
(1, 2, 3)
>>> v1, v2, v3 = a
>>> v1
1
>>> v2
2
>>> v3
3
>>>

HTH
-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list