【问题标题】:TypeError: unbound method set_rank() must be called with Link instance as first argument (got nothing instead)TypeError:未绑定的方法 set_rank() 必须使用 Link 实例作为第一个参数调用(什么都没有)
【发布时间】:2017-03-26 10:02:05
【问题描述】:

当我跑步时 (nohup python -u rerank.py&)

我有以下:

  Traceback (most recent call last):
   File "rerank.py", line 24, in <module>
   rank_all()
   File "rerank.py", line 11, in rank_all
   Link.set_rank()
 TypeError: unbound method set_rank() must be called with Link instance as first argument (got nothing instead)

我的文件 rerank.py 看起来像这样,但我没有找到任何 gem

   #!/usr/bin/env python
   import os
   import django

   os.environ.setdefault("DJANGO_SETTINGS_MODULE", "news_factory.settings")
   django.setup()
   from news.models import Link

   def rank_all():
   for link in Link.with_votes.all():
    Link.set_rank()

    import time


    def show_all():
     print "\n".join("%10s %0.2f" % (l.title, l.rank_score,) for l in 
     Link.with_votes.all())

    print "----\n\n\n"

    if __name__ == "__main__":
     while 1:
      print "---"
      rank_all()
      show_all()
      time.sleep(5)

非常感谢您的帮助

【问题讨论】:

    标签: python django


    【解决方案1】:

    Link.set_rank() 更改为link.set_rank()Link.set_rank(link)

    当前版本在类上查找set_rank,但没有传入link 实例进行操作。我们称之为“未绑定方法”。

    建议的修订要么通过在 link 实例上进行查找来创建“绑定方法”,要么只是将 link 直接传递到未绑定方法中。两种方式都有效,但第一种方式更短、更快且通常更受欢迎。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-24
      • 2017-12-23
      • 1970-01-01
      • 2014-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-10
      相关资源
      最近更新 更多