title: How to call lambda inside the __init__?
body: <p>My big application involves few codes like this:</p>
  
  <pre><code>def __init__(self,master):
      self.destroy= lambda num=num : self.destroywidgets(num)
  
  def destroywidgets(self,num):
      if num == 1:
          for widget in self.frame1.winfo_children():
              widget.destroy()
      elif num == 2:
          for widget in self.frame2.winfo_children():
              widget.destroy()
      elif num == 3:
          for widget in self.frame3.winfo_children():
              widget.destroy()
  
  def func1(self):
      self.destroy(1)
  </code></pre>
  
  <p>This is the small picture of my code. But when I run this code, I am getting below error:</p>
  
  <pre><code>self.destroy= lambda num=num : self.destroywidgets(num)
  NameError: name 'num' is not defined
  </code></pre>
  
  <p>I am really confused as this code was running successfully the previous day. This is throwing such error after restart today. Why is this behaviour?<br>
  Please note that, I tried altering this lambda in multiple ways and referred to many SOF answers, still couldn't resolve it. </p>
  
  <p>Also, please tell me if this could be done using partial(functools).</p>
  
  <p>To make it understand better, below is the smaller snippet I made:</p>
  
  <pre><code>num = ""
  
  def paint(num):
      if num == 1:
          return "1st paint - green"
      elif num == 2:
          return "1st paint - blue"
      elif num == 3:
          return "1st paint - red"
      else:
          return "no more options"
  
  op = lambda num = num : paint(num)
  
  print(op(2))
  print(op(3))
  print(op(4))
  </code></pre>
  
  <p>And I get the perfect output as below:</p>
  
  <pre><code>1st paint - blue
  1st paint - red
  no more options
  </code></pre>
  
  <p>But why isn't this working in above code?</p>