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 getattr(self, "_db", None) is not None:self._db.close()self._db = None
def close(self):"""Closes this database connection."""if hasattr(self, "_db"):self._db.close()self._db = None
yes, i think so. perhaps enhance readability
>
> 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