Q:

Replacing a lambda command in a Tkinter button but giving wrong answer

In my code I have a custom frame class which contains multiple entries and a button that should be able to remove itself from the overall frame. There is a second button that adds a section to a list that manages all of the different sections:
def addSection():
    sectionNumber = len(sections)
    if sectionNumber <= 25:
        sections.append(sectionFrame(SectionsFrame.interior,sectionNumber))
        sections[sectionNumber].pack(side=TOP)
        sections[sectionNumber].getButton().config(command = lambda: removeSpecificSection(sections[sectionNumber].getSectionNumber()))

Now when I remove a section I want to give each section below the one removed the proper place in the list. this is the section button method:
def removeSpecificSection(index):
    sections[index].pack_forget()
    del(sections[index])
    for section in sections[index:]:
        section.getButton().deletecommand(section.getButton().cget("command"))
         section.moveDownIndex()
         section.getButton().config(command = lambda: removeSpecificSection(section.getSectionNumber()))

section.moveDownIndex() is only self.sectionNumber=self.sectionNumber-1 in its own class.
The problem that I'm getting is that if I try to remove any of the sections that were touched by the remove function it will just remove the last section and the index that is coming into the second call confirms that's it always the last index. 
Maybe I don't understand the lambda function, why each button is not taking the its own section for when it calls the remove function ?

A:

This is a very common beginners trap. lambda is late binding, which means it uses the value from sectionNumber when it's called, not from when it was defined. To get the behavior you want you need to use functools.partial: 
from functools import partial
...
sections[sectionNumber].getButton().config(command = partial(removeSpecificSection, sections[sectionNumber])))

and then call the getSectionNumber in the removeSpecificSection method. 

