Application instaskiation vial = Flask ()

I intentionally deleted the name in app = Flask ( name ), and I get this error:

Traceback (most recent call last): File "routes.py", line 4, in <module> app = Flask() TypeError: __init__() takes at least 2 arguments (1 given) 

this is my nettuts code and here is my code:

 from flask import Flask, render_template app = Flask() @app.route('/') def home(): return render_template('home.html') @app.route('/about') def about(): return render_template('about.html') if __name__ == '__main__': app.run(debug=True) 

My question is: where is this init method that takes at least 2 arguments?

+4
source share
6 answers

If you understand the concept of a class and objects, then __init__ is a constructor that initializes an instance of the class. In this case, the Flask class, and when you do the following, you initialize the Flask object instance:

 app = Flask(__name__) 

Now your question: "Where is this init method that takes at least 2 arguments?"

This can be explained in accordance with 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 are a required parameter, and all others are by default or not required. self is required by Python, although you can call it something else. read this blog with the creator of python itself, for which http://neopythonic.blogspot.com/2008/10/why-explicit-self-has-to-stay.html

+10
source

__init__ akin to a constructor in python - its function, called when creating a new instance of the object, in this case, the Flask application.

App objects require import_name , which is the first argument that you pass to the Flask constructor. You can read about it here (see "About the first parameter")

+7
source

My question is: where is this init method that takes at least 2 arguments?

It is right here:

https://github.com/mitsuhiko/flask/blob/master/flask/app.py#L315

+2
source

You need to give the Flask application a name:

 app = Flask(__name__) 

__name__ will be the name of the current module, but you can call it whatever you want in principle ...

+1
source

To answer your question, __init__ is called as a call to Flask() . Your initial call is to run an instance of the Flask class, and __init__ is the function that does the tuning.

To solve your immediate problem: You need to pass only one argument. The error message is misleading.

This is not true, but it is a function that you are not calling. The @codegeek example shows you what the "first" argument is. This is self . But it came from inside the class when class.__init__ is called as a call to Flask() . You do not see self , except implicitly - this is the argument (1 given) in your trace when you thought you were passing null arguments.

It is important to note that this is not unique to this case - you will see the same in very simple examples, for example:

 class Example: def __init__(self, one): self.one = one ex = Example() 

This will give:

 TypeError: __init__() takes exactly 2 arguments (1 given) 

The value, Example() calls __init__ , which wants me and one, and it only got me.

(From the question, however, I strongly recommend that you read something like http://pythoncentral.io/introduction-to-python-classes/ or another introduction to python classes. Classes are a fundamental element of the language and their initialization is the main part of them functionality.)

+1
source

To simply answer your question, the __ init __ method is the default constructor for the Flask () class. It just resides in the Flask class that you just imported from the instruction

 from flask import Flask 

So, the moment you speak

 app = Flask(__name__) 

All he does is simply create an object of the Flask class. And since you did not pass the __ name __ parameter, so it throws an error.

And you can find the __ init __ method here

0
source

Source: https://habr.com/ru/post/1491667/


All Articles