TypeError: __init__() 接受 1 个位置参数,但给出了 3 个

Posted

技术标签:

【中文标题】TypeError: __init__() 接受 1 个位置参数,但给出了 3 个【英文标题】:TypeError: __init__() takes 1 positional argument but 3 were given 【发布时间】:2015-07-13 22:53:26 【问题描述】:

我知道这个标题对于一些老问题看起来很熟悉,但我已经查看了每一个问题,但没有一个能解决。这是我的代码:

TypeError: __init__() takes 1 positional argument but 3 were given

Traceback (most recent call last)
File "C:\Users\hp user\virtual_flask\lib\site-packages\flask\app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Users\hp user\virtual_flask\lib\site-packages\flask\app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "C:\Users\hp user\virtual_flask\lib\site-packages\flask\app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\hp user\virtual_flask\lib\site-packages\flask\_compat.py", line 33, in reraise
raise value
File "C:\Users\hp user\virtual_flask\lib\site-packages\flask\app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\hp user\virtual_flask\lib\site-packages\flask\app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\hp user\virtual_flask\lib\site-packages\flask\app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\hp user\virtual_flask\lib\site-packages\flask\_compat.py", line 33, in reraise
raise value
File "C:\Users\hp user\virtual_flask\lib\site-packages\flask\app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\hp user\virtual_flask\lib\site-packages\flask\app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Users\hp user\PycharmProjects\flask123\views.py", line 19, in create
create_post = Post(my_form.title.data, my_form.text.data)
TypeError: __init__() takes 1 positional argument but 3 were given
The debugger caught an exception in your WSGI application. You can now look at the traceback which led to the error.
To switch between the interactive traceback and the plaintext one, you can click on the "Traceback" headline. From the text traceback you can also create a paste of it. For code execution mouse-over the frame you want to debug and click on the console icon on the right side.

You can execute arbitrary Python code in the stack frames and there are some extra helpers available for introspection:

dump() shows all variables in the frame
dump(obj) dumps all that's known about the object

我的课程如下:

模型.py

from app import db
from datetime import datetime


class Post(db.Model):
    post_id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(100))
    text = db.Column(db.Text())
    created_time = db.Column(db.DateTime())

    def __init__(self, title, text, created_time=None):
        self.title = title
        self.text = text
        if created_time is None:
            self.created_time = datetime.utcnow()
        else:
            self.created_time = created_time

views.py

from app import app, db
from flask import render_template, request, url_for
from forms import CreateForm
from models import Post


@app.route('/')
def homepage():
    return render_template('base.html')


@app.route('/create', methods=['GET', 'POST'])
def create():
    form = CreateForm(csrf_enabled=False)
    if request.method == 'GET':
        return render_template('create.html', form=form)
    if request.method == 'POST':
        if form.validate_on_submit():
            create_post = Post(form.title.data, form.text.data)
            db.session.add(create_post)
            db.session.commit()
    return redirect(url_for('homepage'))

我已经尝试了所有可能的解决方案,并检查了我的代码是否存在拼写错误。但我没有找到。

【问题讨论】:

可能的来源:1. 你的db.Model 基类——不管它是什么——用__init__ 做奇怪的事情,2. 在你的views.py 中,models.Post 不是你所期望的成为(错误的模块?Post 名称稍后在模型或视图模块中被遮蔽?),3. 你的 __init__ 函数在你的 models.py 中缩进严重,所以 Post 使用继承的 __init__,42. 某人在你的代码上加上一个咒语。您是否尝试过检查Post 在您看来到底是什么?就像,你知道的,使用打印语句或调试器? 【参考方案1】:

错误

TypeError: __init__() 接受 1 个位置参数,但给出了 3 个

发生在代码中

create_post = Post(my_form.title.data, my_form.text.data)

不要将位置参数传递给Post对象的创建,而是传递keyword arguments:

create_post = Post(title=my_form.title.data, text=my_form.text.data)

【讨论】:

以上是关于TypeError: __init__() 接受 1 个位置参数,但给出了 3 个的主要内容,如果未能解决你的问题,请参考以下文章

TypeError: __init__() 接受 1 个位置参数,但给出了 3 个

TypeError: module.__init__() 最多接受 2 个参数(给定 3 个)

图表类:TypeError:__init__() 接受 1 个位置参数,但给出了 3 个

TypeError: __init__() 接受 2 个位置参数,但给出了 3 个 // 链接列表 [重复]

TypeError: __init__() 得到了一个意外的关键字参数“评分”

PyTorch - TypeError: forward() 接受 1 个位置参数,但给出了 2 个