Flask instanciation app = Flask()
If you understand the concept of class and objects, then __init__
is the constructor which initializes an instance of the class. In this case, the class is Flask and when you do the following, you are initializing the instance of a Flask object:
app = Flask(__name__)
Now your question, "Where is this init method that takes at least 2 arguments?"
That can be explained as per the definition below that defines the constructor in the code.
def __init__(self, import_name, static_path=None, static_url_path=None, static_folder='static', template_folder='templates', instance_path=None, instance_relative_config=False):
If you see above, self
and import name
is the required parameter and rest are all defaulted or not required. The self
is needed by Python even though you can name it anything else. read this blog by creator of python himself for why http://neopythonic.blogspot.com/2008/10/why-explicit-self-has-to-stay.html
__init__
is akin to a constructor in python - its the function which gets called when you create a new instance of an object, in this case the Flask
application object.
App objects need an import_name
, which is the first argument you pass to the Flask
constructor. You can read more about that here (see "About the First Parameter")
My question is: Where is this init method that takes at least 2 arguments?
It's right here:
https://github.com/pallets/flask/blob/master/src/flask/app.py#L401