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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:59:43+00:00 2026-06-14T08:59:43+00:00

I have a bunch of callback functions in a module. They all call some_func()

  • 0

I have a bunch of callback functions in a module. They all call some_func() with the first, say, couple of arguments always the same and derived from their own arguments, while other arguments vary. Like so:

from outer.space import some_func
def callback_A(param1, param2, param3):
    ...
    some_func(param2+param1, param3, ..., ...)
    ...

def callback_B(param1, param2, param3, param4):
    ...
    some_func(param2+param1, param3, ..., ...)
    ...

And it’s all over the code. And uglier than just param2+param1.

In C/C++, I’d just do a macro

#define    S_F(a,b)    some_func(param2+param1, param3, a, b)

and start using S_F inside callbacks instead of some_func. What can I do in Python?

  • 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-14T08:59:45+00:00Added an answer on June 14, 2026 at 8:59 am

    You can use functools.partial

    >>> from functools import partial
    >>> def my_function(a,b,c,d,e):
    ...     print (a,b,c,d,e)
    ... 
    >>> func_with_defaults = partial(my_function, 1, 2, e=5)
    >>> func_with_defaults(3, 4)
    (1, 2, 3, 4, 5)
    

    Edit:

    Since you do not have these values in advance you can’t use partial or lambda.
    You may be tempted to do this:

    >>> A = lambda x: x + y
    >>> def do_something(y):
    ...     return A(2)    # hope it uses the `y` parameter...
    ... 
    >>> do_something(1)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<stdin>", line 2, in do_something
      File "<stdin>", line 1, in <lambda>
    NameError: global name 'y' is not defined
    

    But as can you see it does not work. Why? Because when you define a function python saves the scope in which you defined it and uses it to resolve the global/nonlocal variables.

    If you had access to some_func it would be possible to do what you want by “hacking” the interpreter stack using inspect but this is not a robust nor elegant thing to do, so do not do that.

    What I’d do in your case is to simply rewrite the statement.

    If you really want to avoid this, you can try something using exec:

    >>> def some_function(a,b,c):
    ...     print(a,b,c)
    ... 
    >>> code = 'some_function(a+b,c,%s)'
    >>> 
    >>> def func_one(a,b, c):
    ...     exec code % 1
    ... 
    >>> def func_two(a,b,c):
    ...     exec code % 2
    ... 
    >>> func_one(1,2,3)
    (3, 3, 1)
    >>> func_two(1,2,3)
    (3, 3, 2)
    

    But this is ugly.

    If you use only positional arguments to your function you may do something more elegant such as:

    >>> def compute_values(a,b,c):
    ...     return (a+b, c)
    ... 
    >>> def func_one(a,b,c):
    ...     some_function(*(compute_values(a,b,c) + (1,)))
    ... 
    >>> def func_two(a,b,c):
    ...     some_function(*(compute_values(a,b,c) + (2,)))
    ... 
    >>> func_one(1,2,3)
    (3, 3, 1)
    >>> func_two(1,2,3)
    (3, 3, 2)
    

    But at this point you are just repeating a different text, and you lost much readability.
    If you want to have preprocessing features in python you may try Python Preprocessing, even though, in your case, I’d rather just repeat the function calls.

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

Sidebar

Related Questions

I have a callback from an ajax call that returns a bunch of image
I have bunch of strings, some of which are fairly long, like so: movie.titles
I have bunch of lists like this: out[3] [[3]] [1] forum=28&mid=11883 [2] forum=29&mid=11884 out[4]
We have bunch of Domain Entities which should be rendered to an html format,
week DKK id=radio2 value=75 /> I have bunch of these div entry generated by
I have a bunch of business class with autoproperties : public class A {
I have a bunch of files in a folder checked out from a repository.
I have a bunch of html files that come with my app and are
I have a bunch of code in a routine that looks a bit like
I have a bunch of strings that are dependent on static dictionaries and each

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.