Q:

Python MySQLdb "with" syntax and DictCursor

I'm trying to use a DictCursor with a with block.  I would think that by using:
with MySQLdb.connect(...) as c:

c would be a connection object, because that's what connect() returns.  But alas, it is not!  All of a sudden, c is a cursor!  While this is usually convenient, I really like using a DictCursor - is this not designed that way at all?  Enclosing the DictCursor as the "scoped object" causes an error (__exit__ wasn't defined)

A:

According to the definition of the Connection in MySQLdb here line 254 of connections.py, Connection.cursor() will return an instance of Connection.cursorclass:
def cursor(self, cursorclass=None):
    """
    Create a cursor on which queries may be performed. The
    optional cursorclass parameter is used to create the
    Cursor. By default, self.cursorclass=cursors.Cursor is
    used.
    """
    return (cursorclass or self.cursorclass)(self)

so, I guess if you initialize the connection with the argument "cursorclass", and set it to MySQLdb.cursors.DictCursor, like:
dbconn = MySQLdb.connect(cursorclass=MySQLdb.cursors.DictCursor)

when it comes
def __enter__(self):
    if self.get_autocommit():
        self.query("BEGIN")
    return self.cursor()

it will return a dict cursor.

