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 7908405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T11:55:08+00:00 2026-06-03T11:55:08+00:00

This answer explains how to create test cases dynamically. The answer’s code: class Tests(unittest.TestCase):

  • 0

This answer explains how to create test cases dynamically.

The answer’s code:

class Tests(unittest.TestCase):
    def check(self, i, j):
        self.assertNotEquals(0, i-j)

for i in xrange(1, 4):
    for j in xrange(2, 6):
        def ch(i, j):
            return lambda self: self.check(i, j)
        setattr(Tests, "test_%r_%r" % (i, j), ch(i, j))

I’ve tested and it works, but I can’t just figure out how?

I have trouble understanding the lambda self: magic in play here, mainly:

  1. Is the lambda used here to perform the exact opposite of functools.partial() (i.e. to create a wrapper function with one extra parameter that is not yet known)
  2. Is self a meaningful keyword or would lambda spam would work just as well?
  3. What point is that lambda evaluated?
  4. How come the .check() is perfectly fine outside the classes scope?
  5. How do you do this without lambda? – if I understand correctly, you should be able to do without lambda (not to mention that I agree with Guido and Alex that it is an eyesore and I want to do without 🙂
  • 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-03T11:55:22+00:00Added an answer on June 3, 2026 at 11:55 am

    First of all: The lambda is returned from a function to prevent that modified values of i and j will be used later.

    Compare to the following:

    for i in xrange(1, 4):
        for j in xrange(2, 6):
            setattr(Tests, "test_%r_%r" % (i, j), lambda self: self.check(i, j))
    

    This will not work as expected, as all the lambdas will share the same namespace and will use the values of i and j after the end of the loop. If we use a separate function call, we introduce a new closure every time that captures the values of i and j at the point in time where the function is called.

    We could achieve the same by saving the current values of i and j as default arguments of the lambda. For good measure, let’s also use itertools.product to avoid the nested loop:

    from itertools import product
    from operator import methodcaller
    
    for i, j in product(range(1, 4), range(2, 6)):
      setattr(Tests, "test_%r_%r" % (i, j),
              lambda self, i=i, j=j: self.check(i, j))
    

    Is the lambda used here to perform the exact opposite of functools.partial() (i.e. to create a wrapper function with one extra parameter that is not yet known)

    Not really. It just calls the check(i, j) method on whatever it is given as an argument. This is used here to dynamically add methods to the Tests class.

    Is self a meaningful keyword or would lambda spam would work just as well?

    spam would work just as well. They choose self here due to convention because the lambda represents a method.

    What point is that lambda evaluated?

    As soon as test_[i]_[j]() is called on an instance of Tests.

    How come the .check() is perfectly fine outside the classes scope?

    Because it’s inside a lambda will only be called later with an instance of Tests as the self argument.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question (along with its answer) explains why you can't easily bind a DataGridView
This answer to a question about C++ unit test frameworks suggests a possibility that
This answer says that Linq is targeted at a slightly different group of developers
This answer says: Vim's undo/redo system is unbeatable. Type something, undo, type something else,
This answer had me slightly confused. What is a 'select to a temp table'
This answer suggested i should put my data in JS instead of a textarea.
This answer seems to show how to make a JSONObject. NSString *jsonString = @[{\id\:
This came up from this answer to a previous question of mine . Is
In this answer to a recent question , I was advised to be wary
In several pieces of sample objective-c code I've seen people create new objects like

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.