#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    346
    Rep Power
    0

    lambda & map


    i am using boa constructor and came across the following:
    Code:
    [wxID_WXFRAME1, wxID_WXFRAME1TOOLBAR1, 
    ] = map(lambda _init_ctrls: wxNewId(), range(2))
    
    [wxID_WXFRAME1TOOLBAR1TOOLS0, wxID_WXFRAME1TOOLBAR1TOOLS1, 
     wxID_WXFRAME1TOOLBAR1TOOLS2, 
    ] = map(lambda _init_coll_toolBar1_Tools: wxNewId(), range(3))
    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.

    Thankyou.
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    206
    Rep Power
    12
  4. #3
  5. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    Originally Posted by yabadabastum
    i am using boa constructor and came across the following:
    Code:
    [wxID_WXFRAME1, wxID_WXFRAME1TOOLBAR1, 
    ] = map(lambda _init_ctrls: wxNewId(), range(2))
    
    [wxID_WXFRAME1TOOLBAR1TOOLS0, wxID_WXFRAME1TOOLBAR1TOOLS1, 
     wxID_WXFRAME1TOOLBAR1TOOLS2, 
    ] = map(lambda _init_coll_toolBar1_Tools: wxNewId(), range(3))
    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.

    Thankyou.
    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.

    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.

    Code:
    >>> a,b,c = [1,2,3] # unpack
    >>> a
    1
    >>> c
    3
    Code:
    >>> map(lambda x: x +10, [1,2,3]) #making a new list from an old one
    [11, 12, 13]
    >>>
    grimey
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    346
    Rep Power
    0
    Thanks for that grim.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    346
    Rep Power
    0
    what is the range() about?

    Also i guess the wxNewID() makes a random number which hasn't been asigned before?

    Thanks
  10. #6
  11. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    Range is one of the Python built-in functions,
    http://docs.python.org/lib/built-in-funcs.html
    it can be used in several ways to create a list.
    e.g.
    >>> range(3)
    [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

    grim
    Last edited by Grim Archon; December 22nd, 2004 at 04:32 AM.

IMN logo majestic logo threadwatch logo seochat tools logo