can anyone enlighten me on the differences between print sth
and print str(sth)
?
E.g. in the example from the official documentation for sqlite3, one can currently see the following code that creates a database and then uses a factory class to wrap data extracted from there:
(1) Create a database:
# I am using CPython 2.7, but I suppose 2.6 will be Ok as well
import sqlite3
conn = sqlite3.connect(":memory:")
c = conn.cursor()
c.execute('''create table stocks
(date text, trans text, symbol text, qty real, price real)''')
c.execute("""insert into stocks values ('2006-01-05','BUY','RHAT',100,35.14)""")
conn.commit()
c.close()
(2) Now using the Row
factory to produce some objects:
>>> conn.row_factory = sqlite3.Row
>>> c = conn.cursor()
>>> c.execute('select * from stocks')
<sqlite3.Cursor object at 0x7f4e7dd8fa80>
>>> r = c.fetchone()
>>> type(r)
<type 'sqlite3.Row'>
>>> r
(u'2006-01-05', u'BUY', u'RHAT', 100.0, 35.14)
As you see, we can type r
, or print r
to get this nice representation of the row object.
But what is not shown above, it is that print str(r)
will give you something different -- something more like:
<sqlite3.Row object at 0x7f4e7dd8abcd>
So I wonder if someone well-acquainted with CPython implementation can explain what print does to obtain this kind of representation from an object that does not support __str__
?
Or I guess an alternative question would be -- in the case when the above expressions give different results -- how I can obtain a string equivalent to the one that would be printed with simple print obj
?