librelist archives

« back to archive

Import / global question

Import / global question

From:
Jurie Horneman
Date:
2012-05-17 @ 15:24
Hi. I've got a weird little situation and while I have a workaround and a 
suspicion about what's causing it, I'd like to understand what is really 
going on.

I've got my Flask application in a module.
In that module, I have an app factory called create_app() which sets a 
global variable (defined in that module) called 'app'. The initial value 
of 'app' is None.

So far, so conventional, I'm sure.

In another module, when I do this:

from application import create_app, app
create_app("dev")

'app' remains None (or, more specifically, it keeps its initial value).

When I do this:

from application import create_app
create_app("dev")
from application import app

'app' has the right value - i.e. the value it is set to inside create_app().

At no point do I actually run a server of any kind. I just create the Flask app.

I expect 'app' to change value in the module that calls create_app. But it
doesn't. I have no idea why this is happening.

I have 3 possible explanations:
- There's some basic, face-palmingly basic Python stuff I am missing, 
which I have never run into before in 10+ years of using Python 
(possible).
- There's some dark, twisted magic going on behind the scenes of Flask. 
But I've read the docs about contexts and thread local stuff and I don't 
understand how that could be causing this.
- It's just plain weird.

Does anyone have an idea?

Thanks,

Jurie

Re: [flask] Import / global question

From:
Audrius Kažukauskas
Date:
2012-05-17 @ 18:02
On Thu, 2012-05-17 at 17:24:23 +0200, Jurie Horneman wrote:
> In another module, when I do this:
> 
> from application import create_app, app
> create_app("dev")
> 
> 'app' remains None (or, more specifically, it keeps its initial
> value).
> 
> When I do this:
> 
> from application import create_app
> create_app("dev")
> from application import app
> 
> 'app' has the right value - i.e. the value it is set to inside
> create_app().
> 
> At no point do I actually run a server of any kind. I just create the
> Flask app.
> 
> I expect 'app' to change value in the module that calls create_app.
> But it doesn't. I have no idea why this is happening.

That's how import in Python works: it defines a new name in a local
namespace which *refers* to imported module or another object.  In your
case both app variables in application module and another module refer
to the same object (None) after import, but they are two *separate*
references.  After you call create_app(), the app variable in
application module now refers to another object (instance of Flask
class), but the other var still holds a reference to None.

-- 
Audrius Kažukauskas
http://neutrino.lt/

Re: [flask] Import / global question

From:
Jurie Horneman
Date:
2012-05-17 @ 20:07
On May 17, 2012, at 8:02 PM, Audrius Kažukauskas wrote:

> On Thu, 2012-05-17 at 17:24:23 +0200, Jurie Horneman wrote:
>> In another module, when I do this:
>> 
>> from application import create_app, app
>> create_app("dev")
>> 
>> 'app' remains None (or, more specifically, it keeps its initial
>> value).
>> 
>> When I do this:
>> 
>> from application import create_app
>> create_app("dev")
>> from application import app
>> 
>> 'app' has the right value - i.e. the value it is set to inside
>> create_app().
>> 
>> At no point do I actually run a server of any kind. I just create the
>> Flask app.
>> 
>> I expect 'app' to change value in the module that calls create_app.
>> But it doesn't. I have no idea why this is happening.
> 
> That's how import in Python works: it defines a new name in a local
> namespace which *refers* to imported module or another object.  In your
> case both app variables in application module and another module refer
> to the same object (None) after import, but they are two *separate*
> references.  After you call create_app(), the app variable in
> application module now refers to another object (instance of Flask
> class), but the other var still holds a reference to None.

And I was so hoping I hadn't asked a really stupid question :)

What you said makes sense. I guess I was thinking too much of pointers…

I'm surprised this hasn't bitten me more often. But I don't usually import
global variables.

Thanks for the explanation!

Jurie