Function that returns a tuple
John Machin
sjmachin at lexicon.net
Sun Jun 17 06:11:11 EDT 2007
On Jun 17, 7:49 pm, Baltimore <alban.lefeb... at gmail.com> wrote:
> On 17 juin, 11:16, Marcpp <mar... at gmail.com> wrote:
>
>
>
> > On 17 jun, 03:53, Dan Hipschman <d... at linux.ucla.edu> wrote:> On Sat, Jun 16, 2007 at 06:30:26PM -0700, Marcpp wrote:
> > > > Hi, I need to returns a tuple from a function (reads a database)
> > > > Any idea?.
>
> > > Like this?
>
> > > def foo():
> > > return 1, 2, 3, 4
>
> > Hi, I need to return a tupla like this function:
>
> > def BDllids(a):
> > a = ()
> > conn = sqlite.connect('tasques.db')
> > cursor = conn.cursor()
> > cursor.execute('SELECT * FROM tasques')
> > for row in cursor:
> > a.append (row[0])
> > return a()
>
> > I'm doing the correct, method?
>
> Why is 'a' used as argument of the function ?
> You don't need to put it in argument.
>
> def BDllids():
> a = ()
> conn = sqlite.connect('tasques.db')
> cursor = conn.cursor()
> cursor.execute('SELECT * FROM tasques')
> for row in cursor:
> a.append (row[0])
> return a
>
> But that's the correct method !
Say what?
| >>> a = ()
| >>> a.append('x')
| Traceback (most recent call last):
| File "<stdin>", line 1, in <module>
| AttributeError: 'tuple' object has no attribute 'append'
| >>>
Instead of
a = ()
the OP should do
a = []
and at the end,
return tuple(a)
Of course if the caller is not being so anal as to check that the
returned gizmoid is actually a tuple, then
return a
would suffice.
Cheers,
John
More information about the Python-list
mailing list