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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:53:14+00:00 2026-06-13T04:53:14+00:00

In Python, for a simple function foo(x, y) there are at least 3 ways

  • 0

In Python, for a simple function foo(x, y) there are at least 3 ways that i know to bind the argument y to some value

# defining a nested function:
def foobar(x):
  return foo(x, y=yval)

# using lambda
foobar = lambda x: foo(x, y=yval)

# using functools
from functools import partial
foobar = partial(foo, y=yval)

while i am doubtful that the list above is exhaustive, i also wonder which one should i go with? are they all equivalent in terms of performance, safety and namespace handling? or are there extra overheads and caveats with each method? why should functools define partial when the other methods are already there?

  • 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-13T04:53:15+00:00Added an answer on June 13, 2026 at 4:53 am

    No, they’re not all equivalent — in particular, a lambda cannot be pickled and a functools.partial can, IIRC, be pickled only in recent Python versions (I can’t find which exact version in the docs; it doesn’t work in 2.6, but it does in 3.1). Neither can functions defined inside of other functions (neither in 2.6 nor 3.1).

    The reason for partial‘s appearance in the library is that it gives you an explicit idiom to partially apply a function inline. A definition (def) cannot appear in the middle of an expression such as

    map(partial(foo, y=yval), xs)
    

    Also, from a definition or lambda, it’s not immediately clear that partial application is what’s going on, since you can put an arbitrary expression in a lambda and arbitrary statements in a definition.

    I suggest you go with partial unless you have a reason not to use it.

    [And indeed, the list is not exhaustive. The first alternative that comes to mind is a callable object:

    class foobar:
        def __init__(self, yval):
            self.__yval = yval
        def __call__(self, x):
            return foo(x, self.__yval)
    

    but those are heavy-weights for such a simple problem.]

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

Sidebar

Related Questions

Is there a simple Python function that would allow unzipping a .zip file like
Here are some simple function calls in python: foo(arg1, arg2, arg3) func1() Assume it
I did simple python function which takes two inputs and output some text. Here
Here's a simple python function that checks if a given url is valid: from
How can I bind arguments to a Python function so that I can call
I have a simple class in python that has a __str__() function. Like this:
Just for fun, I wrote this simple function to reverse a string in Python:
I have a fairly simple python loop that calls a few functions, and writes
I have a simple Python script that uses a signal handler for Ctl-C. If
I have a simple python curses code that creates a subwindow. However, in the

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.