python中的错误提示

何世友 发布于 2011/07/16 14:30
阅读 834
收藏 0

定义函数:

def getCommunityList(self, IDType, ID, type, maxPrice, minPrice, roomNum, startDice, pageCount):

Pass

 

 

调用函数:

communityList = getCommunityList('fieldid', fieldID, type, maxPrice, minPrice, roomNum, startDice, pageCount)

此处如果如此调用,则导致 参数数量错误:

TypeError: getCommunityList() takes exactly 6 arguments (8 given)

 

由于 self 参数的传递是通过 self.funcName()形式调用!

因此,如此调用方显正常:

communityList = self.getCommunityList('fieldid', fieldID, type, maxPrice, minPrice, roomNum, startDice, pageCount)


实在是对self理解不能啊!

望搭救!

加载中
1
周翼翼
周翼翼
#conding=gbk
class Test:
  def foo(self):
    print "foo"

  def bar(self):
    print "bar"
    self.foo()

if __name__=="__main__":
  t=Test()
  t.bar()
#这就是你代码的简写版

1.如果你要communityList = getCommunityList(....)这么调用,getCommunityList是成员函数,你需要一个对象,比如obj.getCommunityList

2.像你在get里面那么调用,那是成员函数调用另一个成员函数,在python里你要显式写上this指针(self).而在c++里,this可加可不加,它会"自内而外"找函数的定义.

0
骠骑将军
骠骑将军
TypeError: getCommunityList() takes exactly 6 arguments (8 given)

说是参数只有6个,你看看是不是有全局函数名字叫
getCommunityList(),只接受6个参数.
0
何世友
何世友

引用来自“骠骑将军”的答案

TypeError: getCommunityList() takes exactly 6 arguments (8 given)

说是参数只有6个,你看看是不是有全局函数名字叫
getCommunityList(),只接受6个参数.

没有全局同名函数。

这个类中只有两个函数。我贴出此函数代码:

# 结合各输入进行分类查询, 按时间先后排序
class GetCommunityListHandler(tornado.web.RequestHandler):
    def get(self):
        result = {"code" : error_code.SUCCESS, "path" : self.request.path} 
        # receive those arguments
        cityID = int(self.get_argument("cityID"))
        countryID = int(self.get_argument("countryID", -1))
        fieldID = int(self.get_argument("fieldID", -1))
        # type ( PreSale, HotSale, ALL)
        type = int(self.get_argument("type", 0))
        maxPrice = int(self.get_argument("maxPrice", 0))
        minPrice = int(self.get_argument("minPrice", 0))
        roomNum = int(self.get_argument("roomNum", -1))
        pageCount = int(self.get_argument("count", 20))
        page = int(self.get_argument("page", 1))
        startDice = (page - 1) * pageCount
        
        if fieldID != -1:
            print "We get field ID %d" % fieldID
            communityList = self.getCommunityList('fieldid', fieldID, type, maxPrice, minPrice, roomNum, startDice, pageCount)
        elif countryID != -1:
            print "We get country ID %d" % countryID
            communityList = self.getCommunityList('countryid', countryID, type, maxPrice, minPrice, roomNum, startDice, pageCount)
        else:
            print "We get city ID %d" % cityID
            communityList = self.getCommunityList('cityid', cityID, type, maxPrice, minPrice, roomNum, startDice, pageCount)
        if not communityList:
            return None
        result["communityList"] = communityList
        print result
        self.write(result)
        
    def getCommunityList(self, IDType, ID, type, maxPrice, minPrice, roomNum, startDice, pageCount):
        # IDType is for the field or country or city id
        database = self.settings["database"]
        print IDType, ID, type, maxPrice, minPrice, roomNum, startDice, pageCount
(以下省略代码1000bit)

如果这里的

if fieldID != -1:
            print "We get field ID %d" % fieldID
            communityList = self.getCommunityList('fieldid', fieldID, type, maxPrice, minPrice, roomNum, startDice, pageCount)
写成

if fieldID != -1:
            print "We get field ID %d" % fieldID
            communityList = getCommunityList('fieldid', fieldID, type, maxPrice, minPrice, roomNum, startDice, pageCount)
则会报错。因为我没有传 self 参数。 只是这个错误有点奇怪,完全不在事实。

我对self这个东西的理解已经完全混乱。以前Java的this的理解还凑合。

 

 

0
何世友
何世友

太感谢 周翼翼 的回答了~!

多谢!

0
何世友
何世友
我觉得python的错误提示太扯了!
返回顶部
顶部