Reputation: 37068
This is the opening code from the Flask mega-tutorial.
Let's start by creating a simple init script for our app package (file app/init.py):
from flask import Flask app = Flask(__name__) from app import views
The script above simply creates the application object (of class Flask) and then imports the views module, which we haven't written yet.
I'm not sure what's going on here. If app
is an instance of a class, how are we using import
on it? The line from app import views
makes no sense to me at all. Could someone help me understand whats going on here? Why would we need to instantiate a class in order to import something?
Upvotes: 1
Views: 71
Reputation: 881805
An unfortunate clash of names! from app import
refers to module or package app
(created just before this code in the tutorial by mkdir app
and editing of this __init__.py
in it), nothing to do with variable name app
which does indeed refer to a class instance.
As the last line of import this
extolls, namespaces are indeed a great thing -- but when they're used implicitly (as in from
and import
, whose namespace is quite separate from a module's) it might be nicer and less confusing to avoid deliberately clashing names anyway... because, if one doesn't, confusion on the reader's part is almost inevitable. Authors of tutorials should be particularly careful about this!
Upvotes: 6