473,222 Members | 1,802 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,222 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 2917
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Birgit Rahm | last post by:
Hallo Newsgroup, I have the following problem: I work with Python 2.2 and invoke functions via CORBA ( I use onmiORB/omniORBpy) on a server. The server provides me a function, where the 3...
1
by: Andrew Wilkinson | last post by:
Hi, First off I know that in almost all cases this would be a terrible thing to do, but this is an unusual case where this makes sense. Basically I have a procedure where you pass a string...
10
by: Doug Jordan | last post by:
I am fairly new to Python. This should be an easy answer but I cannot get this to work. The code is listed below. I know how to do this in C, Fortran, and VB but it doesn't seem to work the same...
1
by: Colin Brown | last post by:
The Python 2.3 documentation in imaplib says: Internaldate2tuple( datestr) Converts an IMAP4 INTERNALDATE string to Coordinated Universal Time. Returns a time module tuple. Time2Internaldate(...
4
by: Michael Yanowitz | last post by:
I am still new to Python but have used it for the last 2+ months. One thing I'm still not used to is that functions parameters can't change as expected. For example in C, I can have status =...
25
by: beginner | last post by:
Hi, I am wondering how do I 'flatten' a list or a tuple? For example, I'd like to transform or ] to . Another question is how do I pass a tuple or list of all the aurgements of a function to...
1
by: Ben Warren | last post by:
Hello, Let's say I have a function with a variable number of arguments(please ignore syntax errors): def myfunc(a,b,c,d,...): and I have a tuple whose contents I want to pass to the...
5
by: Yinghe Chen | last post by:
Hello gurus, I have a question, a function like below, it is implemented by me, :) def funcA(tarray): a = if len(tarray) >=3: return a,a, a elif len(tarray) == 2: return a,a, funcB(1)
1
by: thesinik | last post by:
import random import math t = tuple() def linsearch(key, l): count = 0 index = 0 t = tuple() for i in l:
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.