简体   繁体   English

Python无法识别我的函数的参数数量

[英]Python failing to recognize the number of arguments of my function

Here is a strange behaviour I encountered today running a script in Python 2.7 : 这是我今天在Python 2.7中运行脚本时遇到的奇怪行为:

Code 1 : 代码1:

def pprint_compare_titles (self, ts_numbers = [1], **kwargs ) :
    temp = self.compare_titles ( ts_numbers, **kwargs )
    length = [ len( max( temp[0].keys(), key = len ) ) ]
    def temp_fun ( x, i ) :
        try :
            return self.ts[ts_numbers[i]].titles[x[0]]
        except IndexError :
            return ''
    for i in range( 0, len( temp ) ) :
        length.append( temp_fun( max( temp[i].values(),
            key = lambda x : len( temp_fun( x, i ) ) ) ) )
    for k in temp[0].keys() :
        print( '| {t: <{l}} |'.format( t = k, l = length[0] )
        + ''.join([ ' {t: <{l}} |'.format(
            t = temp_fun( temp[i][k], i ),
            l = length[i+1] )
            for i in range( 0, len(temp) ) ]) )

Output : 输出:

     45         for i in range( 0, len( temp ) ) :
     46             length.append( temp_fun( max( temp[i].values(),
---> 47                 key = lambda x : len( temp_fun( x, i ) ) ) ) )
     48         for k in temp[0].keys() :
     49             print( '| {t: <{l}} |'.format( t = k, l = length[0] )

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

Code 2 : 代码2:

def pprint_compare_titles (self, ts_numbers = [1], **kwargs ) :
    temp = self.compare_titles ( ts_numbers, **kwargs )
    length = [ len( max( temp[0].keys(), key = len ) ) ]
    def temp_fun ( x, i ) :
        try :
            return self.ts[ts_numbers[i]].titles[x[0]]
        except IndexError :
            return ''
    for i in range( 0, len( temp ) ) :
        length.append( temp_fun( max( temp[i].values(),
            key = lambda x : len( temp_fun( x, i, 42 ) ) ) ) )
    for k in temp[0].keys() :
        print( '| {t: <{l}} |'.format( t = k, l = length[0] )
        + ''.join([ ' {t: <{l}} |'.format(
            t = temp_fun( temp[i][k], i ),
            l = length[i+1] )
            for i in range( 0, len(temp) ) ]) )

Output : 输出:

     45         for i in range( 0, len( temp ) ) :
     46             length.append( temp_fun( max( temp[i].values(),
---> 47                 key = lambda x : len( temp_fun( x, i, 42 ) ) ) ) )
     48         for k in temp[0].keys() :
     49             print( '| {t: <{l}} |'.format( t = k, l = length[0] )

TypeError: temp_fun() takes exactly 2 arguments (3 given)

The only difference between these two blocks is the number of arguments I give to temp_fun . 这两个块之间的唯一区别是我给temp_fun的参数temp_fun The second output makes sens but I can't understand the behaviour of the first one, given that Python recognizes 3 parameters on the second. 第二个输出使sens,但我无法理解第一个的行为,因为Python识别第二个上的3个参数。

If anyone has an idea of what is going on, I would gladly take it. 如果有人知道发生了什么,我很乐意接受它。

Looks like the error is actually occurring on the line previous to that one: 看起来错误实际上发生在该行之前的行上:

    length.append( temp_fun( max( temp[i].values(),
                  #^^^^^^^^ here
        key = lambda x : len( temp_fun( x, i ) ) ) ) )

The temp_fun inside your len call has the correct number of arguments, but not the temp_fun inside your append call. len调用中的temp_fun具有正确数量的参数,但不temp_fun append调用中的temp_fun

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM