Python MySQL and context managers: __exit__ Attribute error
<p>Using Python 2.7.9, I'm trying to factor out the opening and closing of mysql connections and cursors</p>

<p>The code is</p>

<pre><code>class MySQLCursor:
    def __init__(self, commit=False):
        self.commit = commit

    def __enter__(self):
        self.conn = MySQLdb.connect(host=_MYMYSQLHOST,
                                    user=_MYMYSQLUSER,
                                    passwd=_MYMYSQLPASSWD,
                                    db=_MYMYSQLDB)
        self.cursor = self.conn.cursor()
        return self.cursor

    def __exit__(self, exc_type, exc_val, exc_tb):
        if self.commit:
            self.conn.commit()
        self.cursor.close()
        self.conn.close()
        return
</code></pre>

<p>using it as </p>

<pre><code>with MySQLCursor as cursor:
    cursor.execute("SELECT VERSION()")
    row = cursor.fetchone()
    print "server version:", row[0]
</code></pre>

<p>I get the error message</p>

<pre><code>AttributeError: __exit__ 
</code></pre>

<p>Is this a mysql problem or a context manager problem?</p>