471,349 Members | 1,260 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,349 software developers and data experts.

Function that returns a tuple

Hi, I need to returns a tuple from a function (reads a database)
Any idea?.

Jun 17 '07 #1
9 2853
On Sat, 16 Jun 2007 18:30:26 -0700, Marcpp wrote:
Hi, I need to returns a tuple from a function (reads a database)
Any idea?.
I don't understand the question. Just return the tuple the same as you
would return an int or a float or a str or any other data type.

def return_a_tuple():
return (1, 2, 3)
--
Steven.

Jun 17 '07 #2
On Sat, 2007-06-16 at 18:30 -0700, Marcpp wrote:
Hi, I need to returns a tuple from a function (reads a database)
Any idea?.
def f():
return (1,2)

Somehow I have the feeling that there's more to your question than
you're letting on...

--
Carsten Haese
http://informixdb.sourceforge.net
Jun 17 '07 #3
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

Jun 17 '07 #4
On 17 jun, 03:53, Dan Hipschman <d...@linux.ucla.eduwrote:
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?

Jun 17 '07 #5
On 17 juin, 11:16, Marcpp <mar...@gmail.comwrote:
On 17 jun, 03:53, Dan Hipschman <d...@linux.ucla.eduwrote: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 !

Jun 17 '07 #6
On 6/17/07, Marcpp <ma****@gmail.comwrote:
On 17 jun, 03:53, Dan Hipschman <d...@linux.ucla.eduwrote:
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?
Tuples are immutable (can't be modified once created). Try this:

def BDllids():
conn = sqlite.connect('tasques.db')
cursor = conn.cursor()
cursor.execute('SELECT * FROM tasques')
a = [row[0] for row in cursor]
return tuple(a)

Is there some particular reason you need to return a tuple as opposed to a list?

P.S. Accessing fields of a SELECT * by numeric index is prone to
breakage if the order of your fields changes. You can make your code
more robust by either specifying the column name explicitly in the
SELECT statement. Alternatively, you may be interested in the
sqlite.Row object (see the documentation for details).

-- David
Jun 17 '07 #7
Baltimore wrote:
On 17 juin, 11:16, Marcpp <mar...@gmail.comwrote:
>On 17 jun, 03:53, Dan Hipschman <d...@linux.ucla.eduwrote: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 = [] # must be a list; tuples don't have an
# append() method
conn = sqlite.connect('tasques.db')
cursor = conn.cursor()
cursor.execute('SELECT * FROM tasques')
Make that "SELECT yadda FROM tasques", assuming that 'yadda' is the first
column in the table.
for row in cursor:
a.append (row[0])
return a

But that's the correct method !
Note that the result is now a list, and that is probably the appropriate
data structure; if you needed a tuple you'd have to convert it:

return tuple(a) # easy, obvious, but probably superfluous

Peter
Jun 17 '07 #8
On Jun 17, 7:49 pm, Baltimore <alban.lefeb...@gmail.comwrote:
On 17 juin, 11:16, Marcpp <mar...@gmail.comwrote:
On 17 jun, 03:53, Dan Hipschman <d...@linux.ucla.eduwrote: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

Jun 17 '07 #9
David Wahler <dw*****@gmail.comwrote:
...
Tuples are immutable (can't be modified once created). Try this:

def BDllids():
conn = sqlite.connect('tasques.db')
cursor = conn.cursor()
cursor.execute('SELECT * FROM tasques')
a = [row[0] for row in cursor]
return tuple(a)

Is there some particular reason you need to return a tuple as opposed to a
list?
Assuming there is (need to use it as a dict key, RHS operand of string
formatting % operator, etc) I would suggest collapsing the last 2
statements in your excellent code into
return tuple(row[0] for row in cursor)

P.S. Accessing fields of a SELECT * by numeric index is prone to
breakage if the order of your fields changes. You can make your code
more robust by either specifying the column name explicitly in the
SELECT statement. Alternatively, you may be interested in the
Absolutely true -- in this case, return tuple(cursor) will suffice.
Alex
Jun 17 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Birgit Rahm | last post: by
1 post views Thread by Andrew Wilkinson | last post: by
10 posts views Thread by Doug Jordan | last post: by
1 post views Thread by Colin Brown | last post: by
4 posts views Thread by Michael Yanowitz | last post: by
1 post views Thread by Ben Warren | last post: by
5 posts views Thread by Yinghe Chen | last post: by
1 post views Thread by thesinik | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.