an advice about tornado.database

34 views
Skip to first unread message

贾晓磊

unread,
Jan 9, 2012, 9:50:01 PM1/9/12
to python-...@googlegroups.com, dmi...@gmail.com
hi, all

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:


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

Is someone agree me?

#NOTE: first time give an advice to tornado. maybe in the following days, i will send more thinking about tornado.

-- Jia Xiaolei

smallfish

unread,
Jan 9, 2012, 9:53:42 PM1/9/12
to python-...@googlegroups.com, dmi...@gmail.com
yes, i think so. perhaps enhance readability
--


2012/1/10 贾晓磊 <jiaxiaole...@gmail.com>

贾晓磊

unread,
Jan 9, 2012, 10:00:56 PM1/9/12
to python-...@googlegroups.com


2012/1/10 smallfish <smallf...@gmail.com>

yes, i think so. perhaps enhance readability

hehe, thanks for your response.
I read your blog in "http://chenxiaoyu.org". Good easy!  nginx+tornado are also be used in our projject and they work perfect.
an extra question: how to give advices or bugs to the maintainer of tornado. it seems terrible to use  "http://www.apache.org/licenses/LICENSE-2.0".

Nice to meet you!

-- Jia Xiaolei



--
NAME: 贾晓磊/Jia Xiaolei
MOBILE: 13011292217
QQ: 281304051

smallfish

unread,
Jan 9, 2012, 10:09:49 PM1/9/12
to python-...@googlegroups.com
see more of github issue page (https://github.com/facebook/tornado/issues?sort=created&direction=desc&state=open)
or add the maillist feedback.
i'm new user :)
--

Cliff Wells

unread,
Jan 9, 2012, 10:26:33 PM1/9/12
to python-...@googlegroups.com
On Tue, 2012-01-10 at 10:50 +0800, 贾晓磊 wrote:

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

Actually that's not the same test at all. hasattr(self, "_db") will be
True even if self._db == None.

>>> class Foo (object):
... def __init__ (self):
... self.bar = None
...
>>> f = Foo ()
>>> hasattr (f, "bar")
True
>>>

Therefore you must have getattr (self, "_db") == None. Except perhaps
there is no attribute _db, which will throw an exception, so you must
have a default:

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

And now you are back to the original code.

Regards,
Cliff


贾晓磊

unread,
Jan 9, 2012, 10:41:32 PM1/9/12
to python-...@googlegroups.com
sorry for my fault and misunderstand. I did not think it deeply and it's all my fault.
thanks for the points. Thanks! 
Reply all
Reply to author
Forward
0 new messages