December 21st, 2004, 06:38 AM
lambda & map
i am using boa constructor and came across the following:
i was wondering what this meant as i am new to python and lambda / map syntax. I am experienced at other languages so know alot about arrays etc - i am guessing these are similar to arrays.
] = map(lambda _init_ctrls: wxNewId(), range(2))
] = map(lambda _init_coll_toolBar1_Tools: wxNewId(), range(3))
December 21st, 2004, 07:46 AM
December 21st, 2004, 08:18 PM
This apprently strange arrangement of lists and functions is the way Boa Constructor programmatically maintains it's widget resources. If you add a new widget - the list of IDs on the left grows by 1 and the range on the right of the equation increments by 1.
Originally Posted by yabadabastum
Boa arranges things so that the map function on the right creates a list with the same number of elements as the list on the left. Python will assign each element on the right to each variable name on the left in a one-to-one relationship. (This is known as list unpacking).
In detail - The range function creates a list of N elements. The map function will apply the specified function to each element of the list to create a new list. In this case Boa is using a lambda function which is sort of in line function that saves you the need to define a function elsewhwere in the code.
The lambda function in these cases have an unused dummy variable - the real meat is the wxNewID function that is called.
So the result is that each variable name on the left is assigned a new wxID.
>>> a,b,c = [1,2,3] # unpack
>>> map(lambda x: x +10, [1,2,3]) #making a new list from an old one
[11, 12, 13]
December 22nd, 2004, 03:31 AM
December 22nd, 2004, 03:32 AM
what is the range() about?
Also i guess the wxNewID() makes a random number which hasn't been asigned before?
December 22nd, 2004, 04:27 AM
Range is one of the Python built-in functions,
it can be used in several ways to create a list.
[0, 1, 2]
Why guess what wxNewID does - if you haven't already done so you should download the helpful documentation that is available from the Boa Constructor site - it provides searchable wxPython documentation integrated into Boa
Last edited by Grim Archon; December 22nd, 2004 at 04:32 AM.