473,396 Members | 2,021 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,396 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 2926
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:
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.