define function in function

Bjorn Pettersen bjorn at roguewave.com
Wed Aug 2 16:44:31 EDT 2000


see below (the lhs dictionary is in "function"'s scope and the rhs is in
sortf's scope).

-- bjorn

Thomas Thiele wrote:
> 
> I have a problem witing a sort function within a memberfunction.
> 
> class X:
>     .......
>     def function(self, dictionary):
> 
>         list = []
>         ...set list entries...
> 

>         def sortf(lx, ly, dictionary=dictionary):

>             return cmp( dictionary[lx],  dictionary[ly] )
> 
>         list.sort(sortf)
> 
> The dict includes some integer numbers as values. The list shall be
> sortet by using this numbers.
> How can I realize that dictionary is know in sortf ?
> delaring dictionary as global in the function or saying d = dictionary,
> and declaring d as global and using d, all don't work.
> 
> Can you help me? How is the trick?
> 
> --
> http://www.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list