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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:36:48+00:00 2026-05-31T19:36:48+00:00

How can I define anonymous functions in python, where the bahaviour should depend on

  • 0

How can I define anonymous functions in python, where the bahaviour should depend on the value of a local variable at definiton-time, and also accept arguments

Example:

def callback(val1, val2):
   print "{0} {1}".format(val1, val2)

i = 0
f0 = lambda x: callback(i, x)
i = 1
f1 = lambda x: callback(i, x)

f0(8) # prints "1, 8: but I'd like "0, 8" (value of 'i' when f0 was defined)
f1(8) # prints "1, 8"

Is something like this possible without wrapping my callback in its own a class?

  • 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-05-31T19:36:49+00:00Added an answer on May 31, 2026 at 7:36 pm

    Closures in python using functools.partial

    from functools import partial
    
    i = 0
    f0 = partial(callback, i)
    i = 1
    f1 = partial(callback, i)
    
    f0()
    # 0
    f1()
    # 1
    

    partial is like a lambda but wraps the value at that moment into the arg. Not evaluating it when its called.

    Wrapping only some of the args

    Yes partial will allow you to wrap any number of the arguments, and the remaining args and kwargs can then be passed to the resulting partial object so that it acts like it was calling the original wrapped function…

    def callback(val1, val2):
       print "{0} {1}".format(val1, val2)
    
    i = 0
    x = 8
    f0 = partial(callback, i)
    f0(x)
    # 0 8
    

    Essentially you have wrapped callback(val1, val2) into callback(val2) with val1 being included as a closure already.

    Example of similar effect using lambda

    In case you really want to see how to do this with a lambda closure, you can see why it gets ugly and partial is preferred…

    f0 = (lambda val1: lambda val2: callback(val1, val2))(i)
    

    You have to wrap the scope variable into an outer function scope, and then reference that scope in the inner lambda function. Yuk.

    Tracebacks from exceptions: partial vs lambda vs nested functions

    With the influx of other answers, I thought I would outline one more reason to use partial as opposed to lambda, or a inner/outer function closure. Keep in mind I mean a function closure. functools.partial fixes the traceback you will get when your wrapped function raises an exception…

    Consider this version that will raise a division by zero:

    def callback(val1, val2):
        return val1 / val2
    

    Normal outter/inner closure

    def wrapper(fn, val1):
        def wrapped(val2):
                return fn(val1, val2)
        return wrapped
    
    f0 = wrapper(callback, i)
    f0(0)
    
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<stdin>", line 3, in wrapped
      File "<stdin>", line 2, in callback
    ZeroDivisionError: integer division or modulo by zero
    

    lambda closure

    f0 = (lambda val1: lambda val2: callback(val1, val2))(i)
    f0(0)
    
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<stdin>", line 1, in <lambda>
      File "<stdin>", line 2, in callback
    ZeroDivisionError: integer division or modulo by zero
    

    And now for functools.partial

    f0 = partial(callback, i)
    f0(0)
    
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<stdin>", line 2, in callback
    ZeroDivisionError: integer division or modulo by zero
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In JavaScript, you can define anonymous functions that are executed immediately: (function () {
I can define default value in domain by this way : class ProcessingPriority {
In Python I can define a function as follows: def func(kw1=None,kw2=None,**kwargs): ... In this
Python doesn't support complicated anonymous functions. What's a good alternative? For example: class Calculation:
I can define an event like this(declared function): MyElement.Keyup +=MyDeclaredFunction I can also define
How can I pass anonymous types as parameters to other functions? Consider this example:
How can I tell closure compiler that an anonymous function should not be removed
In Java you can define a new class inline using anonymous inner classes. This
In C# you can define delegates anonymously (even though they are nothing more than
I can define church numerals fairly easy using scheme: > (define f (lambda (x)

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.