[Tutor] Declaring variables

Alan Gauld alan.gauld at btinternet.com
Thu Apr 7 19:17:23 EDT 2016


On 07/04/16 18:49, Dimitar Ivanov wrote:

> As far as my understanding goes, MySQLdb function called 'fetchone()'
> returns the result as a tuple. Problem is, the tuple has some unnecessary
> characters, such as an additional comma being returned. In my case:

The comma is what makes it a tuple, its not an extra character.
It's like the quotes around a string, they are what defines it
to be a string. The tuple is defined by having its members
separated by commas (not as some think by having parens
around them!). So how do you represent a tuple of one element?
By adding a trailing comma, which is what you are seeing.

x,y = (1,2)

the two expressions, on each side of the equals are both tuples. One
with parens, one without. But they are both tuples. The parens are
really only needed to avoid ambiguity.

>>>>  idquery = 'select id from table;'
>>>>  cur = mysql.cursor()
>>>>  cur.execute(idquery)
>>>>  id = cur.fetchone()
>>>>  print id
> ('idinhere',)
> 
> So I decided to give it a try and the result is exactly what I need:
> 
>>>>  (id,) = cur.fetchone()
>>>>  print id
> idinhere

Yes, or you could just have printed id[0] - the first(only)
element of the tuple.

>>> t = (3,)
>>> type(t)
<class 'tuple'>
>>> print (t)
(3,)
>>> print(t[0])
3
>>>

> My question is - can I reliably use this method? Is it always going to
> return the string between the brackets 

It will always return a tuple containing as many element as you request.
In this case one.

> with '(,)'? I'm planning to use another query that will be using the result
> from this one and then update another database with this result but I must
> be sure that the variable will always be the string in and between the
> brackets otherwise I'm risking to mess up a lot of things big time.

Provided you extract the values from the tuple they will be of
whatever type the database returns. In this case its a string
but it could be an integer or float too. Just extract them
from the tuple using indexing as you would do for any other tuple.

> A backup plan I had was to use the following:
> 
>>>>  id = cur.fetchone()
>>>>  for x in id:
>>>>    id = x

Again that will work, but the simplest option is just to use indexing

id = id[0]

in your case. Or just use id[0] directly wherever you need the
value.

> Also, just to clarify things for myself - what does this method of
> declaring variables do exactly? 

x,y = 3,4

Is known as tuple unpacking and is equivalent to

x = 3
y = 4

or expressing it slightly differently

tup = (3,4)
x,y = tup

is equivalent to

x = tup[0]
y = tup[1]


it assigns the elements of the tuple on the right to the elements
of the tuple on the left. So in your case you are just doing the
same thing but with single valued tuples

x, = t  # where t = (3,)

in this case equivalent to saying

x = t[0]

> I'm sorry if this isn't the right place the
> ask and if this has been documented clearly already, I'm not sure what to
> use as a search term in order to find an answer.

It's exactly the right place to ask and it has been documented,
the trick is to know to search for tuple unpacking! :-)


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list