Re: [flask] extension development example - calling property object
- 2013-01-31 @ 16:11
On Wed, Jan 30, 2013 at 1:38 AM, Steven Kryskalla <email@example.com>wrote:
> On Tue, Jan 29, 2013 at 9:23 PM, Shantanu <firstname.lastname@example.org> wrote:
> > I am trying to follow SQLite3 example in extension development
> > . It has defined 'connection' property in the example code which is
> > used as a callable object in later usage examples (e.g.
> > db.connection.cursor()). Wouldn't such calls return 'object is not
> > type errors? I am new to Python, so not sure about it. Any help?
> > 1. http://flask.pocoo.org/docs/extensiondev/#the-extension-code
> That code is using a property for db.connection. Properties allow you
> to override attribute lookup (and update/delete) to do extra behavior.
> That code for db.connection calls sqlite3.connect(...) the first time
> it's accessed, stores that in the current app context, and returns it.
> Subsequent calls return the same sqlite3 connection object from the
> app context, so it doesn't have to reconnect each time.
> Does that help explain it? If not ask more questions and experiment
> with @property yourself to see how it works :)
Thank you for explaining it Steve. I had read about property decorator,
but I hadn't understood it completely. I used it to set, get and delete an
attribute/object, but didn't think about calling further methods on it.