librelist archives

« back to archive

extension development example - calling property object

extension development example - calling property object

From:
Shantanu
Date:
2013-01-30 @ 05:23
I am trying to follow SQLite3 example in extension development
documentation [1]. It has defined 'connection' property in the example code
which is being used as a callable object in later usage examples (e.g. db.
connection.cursor()). Wouldn't such calls return 'object is not callable'
type errors? I am new to Python, so not sure about it. Any help?

1. http://flask.pocoo.org/docs/extensiondev/#the-extension-code

--
Thanks,
Shantanu

Re: [flask] extension development example - calling property object

From:
Steven Kryskalla
Date:
2013-01-30 @ 07:38
On Tue, Jan 29, 2013 at 9:23 PM, Shantanu <knowshantanu@gmail.com> wrote:
> I am trying to follow SQLite3 example in extension development documentation
> [1]. It has defined 'connection' property in the example code which is being
> used as a callable object in later usage examples (e.g.
> db.connection.cursor()). Wouldn't such calls return 'object is not callable'
> 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.

http://docs.python.org/2/library/functions.html#property

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 :)

-Steve

Re: [flask] extension development example - calling property object

From:
Shantanu
Date:
2013-01-31 @ 16:11
On Wed, Jan 30, 2013 at 1:38 AM, Steven Kryskalla <skryskalla@gmail.com>wrote:

> On Tue, Jan 29, 2013 at 9:23 PM, Shantanu <knowshantanu@gmail.com> wrote:
> > I am trying to follow SQLite3 example in extension development
> documentation
> > [1]. It has defined 'connection' property in the example code which is
> being
> > used as a callable object in later usage examples (e.g.
> > db.connection.cursor()). Wouldn't such calls return 'object is not
> callable'
> > 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.
>
> http://docs.python.org/2/library/functions.html#property
>
> 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.

--
Shantanu