Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7936811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:19:35+00:00 2026-06-03T22:19:35+00:00

Currently, my code is like this: for control in self.controls(): self.connect(control, SIGNAL(‘clicked()’), lambda: self.button(control.objectName()))

  • 0

Currently, my code is like this:

for control in self.controls():
  self.connect(control, SIGNAL('clicked()'), lambda: self.button(control.objectName()))

When I run my code, my lambda function takes control to be the last element in my list of elements, which is not what I expect to happen.

Oddly enough, when I manually run the lambda every loop, it works fine for each loop, but it still has the same problem as before in the end:

for control in self.controls():
  func = lambda: self.button(control.objectName())
  func() # Prints the correct output

  self.connect(control, SIGNAL('clicked()'), func) # When triggered, still breaks

My self.button() function is trivially defined:

def button(self, name):
    print name

I checked for the uniqueness of control and func every loop by printing the output of id(control) and id(func) and they all returned unique numbers. Aside from that, control.objectName() is the correct object name.

I have a feeling that this is a variable scope problem, as I usually don’t use lambdas too often in my code. Does anybody see any blatant mistakes?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T22:19:36+00:00Added an answer on June 3, 2026 at 10:19 pm

    SPLAT.

    for control in self.controls():
      self.connect(control, SIGNAL('clicked()'), lambda control=control:
        self.button(control.objectName()))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using dotnet 2.0. I currently have code like this : DataView dv = new
My code currently looks like this: private Foo myFoo; public Foo CurrentFoo { get
My code currently looks like this: <div style=position: fixed; width: 35.25%; height: 6.75%; left:
I have an Entity in Code First Entity framework that currently looks like this:
I am currently making a lot of use of code like this in order
I am currently using PHP_Beautifier for formatting code with the following command like options
I'm currently using the validation code listed here in an application. I'd like to
Currently my code below works fine but it's a bit of overkill. In my
Currently my code loads a TabWidget with 4 tabs. The first tab points to
Currently our code uses a for-loop for filling a buffer holding a Unicode string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.