V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
miniyao
V2EX  ›  Python

在学习 Flask web 教程里, super(User, self).__init__(**kwargs) 为什么一直被 IDE 提示错误: unexpected argument?

  •  
  •   miniyao · 2017-09-26 22:17:59 +08:00 · 3031 次点击
    这是一个创建于 2397 天前的主题,其中的信息可能已经有所发展或是发生改变。

    用 Pycharm 看的这个代码,super(User, self).__init__(**kwargs) 中的 ···**kwargs``` 部分一直被 Pycharm 提示:unexpected argument

    #models.py

    class User(UserMixin, db.Model):
        __tablename__ = 'users'
        id = db.Column(db.Integer, primary_key=True)
        email = db.Column(db.String(64), unique=True, index=True)
    
        ... ...
    
        def __init__(self, **kwargs):
            super(User, self).__init__(**kwargs)  # 这里 IDE 提示 **kwargs:unexpected argument
            ... ...
            self.followed.append(Follow(followed=self))
    
    7 条回复    2017-09-28 08:49:57 +08:00
    cxbats
        1
    cxbats  
       2017-09-27 00:15:41 +08:00 via Android
    因为你忘了传 self
    zwgmlr3
        2
    zwgmlr3  
       2017-09-27 13:11:28 +08:00 via Android
    不妨看一下语法 ** 是什么意思?
    miniyao
        3
    miniyao  
    OP
       2017-09-27 14:38:50 +08:00
    @cxbats
    @zwgmlr3

    感觉这个像是 Pycharm 的 bug,对 super()继承父类的参数识别不准确吧。有了解的同学,请指正。
    wangkai123
        4
    wangkai123  
       2017-09-27 16:00:00 +08:00
    **kwargs->kwargs
    wangkai123
        5
    wangkai123  
       2017-09-27 16:03:10 +08:00
    好像没有用 hhh
    zwgmlr3
        6
    zwgmlr3  
       2017-09-27 16:16:22 +08:00
    看一下你的 UserMixin 和 db.Model 的 __init__ 方法怎么定义的,下面这种写法肯定会报错啊
    class UserMixin():
    def __init__(self):
    pass


    class User(UserMixin):
    __tablename__ = 'users'

    def __init__(self, **kwargs):
    super(User, self).__init__(**kwargs)
    zhusimaji
        7
    zhusimaji  
       2017-09-28 08:49:57 +08:00
    老铁没毛病啊,这个应该是你 pycharm 的问题
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2570 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 15:31 · PVG 23:31 · LAX 08:31 · JFK 11:31
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.