5

我故意删除了 app = Flask( name ) 中的name,我收到了这个错误:

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

这是我来自nettuts的代码,这是我的代码:

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)

我的问题是:这个至少需要 2 个参数的init方法在哪里?

4

6 回答 6

10

如果您了解类和对象的概念,那么__init__就是初始化类实例的构造函数。在这种情况下,类是 Flask,当您执行以下操作时,您正在初始化 Flask 对象的实例:

app = Flask(__name__) 

现在你的问题是,“这个至少需要 2 个参数的 init 方法在哪里?”

这可以根据下面定义代码中的构造函数的定义来解释。

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

如果你在上面看到,selfandimport name是必需的参数,其余的都是默认的或不需要的。self即使您可以将其命名为其他任何名称,Python 也需要它。阅读 python 创建者本人的这篇博客,了解为什么http://neopythonic.blogspot.com/2008/10/why-explicit-self-has-to-stay.html

于 2013-07-16T15:00:44.387 回答
7

__init__类似于 python 中的构造函数 - 它是在创建对象的新实例时调用的函数,在本例中是Flask应用程序对象。

App 对象需要一个import_name,这是您传递给Flask构造函数的第一个参数。您可以在此处阅读更多相关信息(请参阅“关于第一个参数”)

于 2013-07-16T14:30:09.400 回答
2

我的问题是:这个至少需要 2 个参数的 init 方法在哪里?

就在这里:

https://github.com/pallets/flask/blob/master/src/flask/app.py#L401

于 2013-07-16T14:43:40.450 回答
1

您需要为 Flask 应用程序命名:

app = Flask(__name__)

__name__将是当前模块的名称,但原则上您可以随意称呼它...

于 2013-07-16T14:28:02.830 回答
1

To directly answer your question: __init__ is called as a consequence of calling Flask(). Your original call is meant to initiate an instance of the class Flask, and __init__ is the function that does the setup.

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

It's not incorrect but it's talking about a function that's not the one you think you called. @codegeek 's example shows you what the "first" argument is. It's self. But that's passed from inside the Class when class.__init__ is called as a consequence of calling Flask(). You don't see self being used, except for implicitly -- it's the (1 given) argument in your traceback, when you thought you were passing zero arguments.

Importantly, this isn't unique to this case -- you'll see the same thing in very simple examples like:

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

This will produce:

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

Meaning, Example() calls __init__ which wants 'self' and 'one', and it only got 'self'.

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

于 2015-12-28T22:28:08.107 回答
0

为了简单地回答您的问题,__init__方法是此处 Flask() 类的默认构造函数。它只是驻留在您刚刚从语句中导入的 Flask 类中

from flask import Flask

所以,你说的那一刻

app = Flask(__name__)

它所做的只是实例化这个 Flask 类的一个对象。而且由于您没有传递__name__参数,因此它会引发错误。

你可以在这里找到__init__方法

于 2016-04-09T19:53:32.047 回答