Sort with extra variables

Thomas Dybdahl Ahle lobais at gmail.com
Fri Mar 2 20:34:25 EST 2007


Den Fri, 02 Mar 2007 16:27:47 -0800 skrev MonkeeSage:

> On Mar 2, 5:51 pm, Thomas Dybdahl Ahle <lob... at gmail.com> wrote:
>> I guess the thing is that I'd have to create a new callable no matter
>> how, as it is the only way to bring the extra variables into the
>> getValue function when called by sort.
> 
> Yes, but you don't have to create it every time you call sortMoves...
> 
> def sortKey(move):
>     return getMoveValue(board, table, ply, move)
> 
> def sortMoves(board, table, ply, moves):
>     moves.sort(key=sortKey, reverse=True) return moves

Well, you'd have to define the function inside the sortMoves function, as 
it is where the variables exists.

def sortMoves(board, table, ply, moves):
    def sortKey(move):
        return getMoveValue(board, table, ply, move)
    moves.sort(key=sortKey, reverse=True) return moves

Wouldn't that make it create the callable at each call?



More information about the Python-list mailing list