Reputation: 133
I am working on a function to get the information from the database so I can get the return values. I am trying to find out how to get each value from the UpdateEPG
function at a time.
When I try this:
def UpdateEpg(self, program_title = '', program_start = '', program_stop = ''):
conn = database.connect(self.profilePath)
cur = conn.cursor()
cur.execute("SELECT title, start_date, stop_date FROM programs where channel=? LIMIT 2", [channel])
program = cur.fetchall()
for ind, row in enumerate(program):
program_title = row[0]
program_start_date = str(row[1])
program_stop_date = str(row[2])
return program_title, program_start_date, program_stop_date
self.UpdateEpg()
print self.UpdateEPG(program_title)
print self.UpdateEPG(program_start_date)
print self.UpdateEPG(program_stop_date)
It will not let me to get the single value from UpdateEpg
. I want to get the single value so I could input in the object to set the label. Can you show me an example how I could get program_title
, program_start_date
and program_stop_date
for each variable to get access to UpdateEpg
to get the information I am looking for?
Upvotes: 1
Views: 294
Reputation: 425
Looks like you're new to Python. And to programming in general. This is not bad in any way; even the best of us have to start somewhere. Here are some tips from someone who's been using Python since he was 12. (Okay, that's only three years or so, but still...)
Firstly, I would strongly reccomend using Python 3 rather than Python 2 if you can help it. Python 2 has been deprecated for four years, and this program will work on Python 3 with little to no modification.
Secondly, in Python, everything is pass-by-value. Assigning to a variable passed into a function will not change that variable's value outside the function, e.g.:
>>> def myfunc(a):
... a=2
...
>>> a = 1
>>> myfunc(a)
>>> print(a)
1
>>>
(Note that at the interactive prompt, most calls to print()
are unneccessary. Simply typing in the name of a variable (or an expression such as 1+1
or someFunction()
) will print its value.)
The exception to this rule is that some objects (such as lists) are mutable. Their contents (but not the lists themselves) can be changed from inside a function:
>>> def myfunc2(l):
... l[0] = 2
...
>>> mylist = [1, 2, 3]
>>> myfunc2(mylist)
>>> mylist
[2, 2, 3]
>>>
Note that the myfunc function did not change the list itself, it only changed a value inside the list.
Strings do not fall in this category. In Python, strings are immutable. Once their contents are set, they cannot be changed. The only way to change the value of a string is to assign the variable a new value, making it forget about the old one.
Thirdly, in Python, functions can return multiple values as a tuple, which is like a list but it uses parentheses (which are optional and can be omitted) instead of square brackets, and its contents cannot be changed (calling myfunc2 from above on a tuple would cause an error). Another handy feature is tuple unpacking (which also works on lists and any other kind of sequence), as follows:
>>> mytuple = (1, 2, 3)
>>> mytuple
(1, 2, 3)
>>> a, b, c = mytuple
>>> a
1
>>> b
2
>>> c
3
>>>
Here is a version of UpdateEpg()
that takes these things into account:
def UpdateEpg(self):
conn = database.connect(self.profilePath)
cur = conn.cursor()
cur.execute("SELECT title, start_date, stop_date FROM programs where channel=? LIMIT 2", [channel])
row = cur.fetchone()
program_title = row[0]
program_start_date = str(row[1])
program_stop_date = str(row[2])
return program_title, program_start_date, program_stop_date
program_title, program_start, program_finish = self.UpdateEpg()
Sorry about the wall of text, but I want to make sure people understand what their program is doing before they just copy-paste code off of StackOverflow.
Upvotes: 0
Reputation: 27684
accessing multiple values is similar to returning multiple values. Just put the names on the left side of =
program_title, program_start_date, program_stop_date = self.UpdateEpg()
print program_title
print program_start_date
print program_stop_date
Upvotes: 1
Reputation: 713
Since your method returns a tuple, you can use indexing to get the desired value.
my_tuple = self.UpdateEpg()
print my_tuple[0] # Prints program_title
print my_tuple[1] # Prints program_start_date
print my_tuple[2] # Prints program_stop_date
Of course, you can use and print the tuple any way you like. Note that you need to call self.UpdateEpg()
inside the class that you declared.
Upvotes: 1