Here is a strange behaviour I encountered today running a script in Python 2.7 :
Code 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 :
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
. 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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.