facebook/tornado — Issue #433: an advice about tornadow.database
Author: jaixiaolei
Created: 2012-01-10 03:12:20+00:00
State: open

I have been using tornado for nearly 2 years! Now, I begin to read the resource code, and find some code can be more beautiful. show a instance as follows:

In the page “https://github.com/jaixiaolei/tornado/blob/master/tornado/database.py”，we can find the code:

def close(self):

"""Closes this database connection."""

if getattr(self, "_db", None) is not None:

self._db.close()

self._db = None

in my views, if getattr(self, "_db", None) is not None: can be replaced to if hasattr(self,"_db"):

def close(self):

"""Closes this database connection."""

if hasattr(self, "_db"):

self._db.close()

self._db = None