Python Forum

Full Version: difference between today, and today variables
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi All,

I want to know what is the difference between variables " today," and "today" in the below example. First option is giving output in date format, while second one is not . Just by adding "," after the variable name, its printing correct format. both are holding tuple data.

>>> cursor.execute("select sysdate from dual")
<cx_Oracle.Cursor on <cx_Oracle.Connection to hr@localhost:1521>>
>>> today, = cursor.fetchone()
>>> print("The current date is", today)
The current date is 2018-11-25 03:36:57


>>> cursor.execute("select sysdate from dual")

<cx_Oracle.Cursor on <cx_Oracle.Connection to hr@localhost:1521>>
>>> today= cursor.fetchone()
>>> print("The current date is", today)
The current date is (datetime.datetime(2018, 11, 25, 3, 37, 52),)

>>> type (today)
<class 'tuple'>
>>> type (today,)
<class 'tuple'>
Please use python and output tags when posting code and results. I put them in for you this time. Here are instructions for doing it yourself next time.

cursor.fetchone is returning a tuple of length 1. By using today = (no comma), you just get the tuple. But using today, = (with a comma) is using tuple assignment. As in:

>>> nums = (1, 2)
>>> one, two = nums
>>> one
1
>>> two
2
And they are not the same type. You are just checking the type of the second today twice. If you had check the type of the first today before reassigning it, you would see it was equal to datetime. Here's an example:

>>> one = (1,)
>>> one
(1,)
>>> uno, = (1,)
>>> uno
1
>>> type(one)
<class 'tuple'>
>>> type(uno)
<class 'int'>
Hi, Thanks for the explanation i got it now.