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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T07:16:09+00:00 2026-06-07T07:16:09+00:00

This is a bit tricky, but what I want, in a simple way, is

  • 0

This is a bit tricky, but what I want, in a simple way, is this:

I have a function foo and a decorator process

@process
def foo(x, y):
    print x + y

The decorator is creating a Process object, which is handed the foo function and it’s parameters

# The process decorator
def process(func=None, **options):
    if func != None:
        def _call(*args, **kwargs):
            if len(options) > 0 and 'fail' in options:
                return Process(func, fail=options['fail'], *args, **kwargs)
            else:
                return Process(func, fail=None, *args, **kwargs)
        return _call
    else:
        def _func(func):
            return process(func, **options)
        return _func

I want to change the foo function given to the process-decorator on run-time, to include a call to another function, e.g. a get_locals() function, which possibly overwrites the local variables, at the very beginning of foo:

def foo(x, y):
    get_locals()
    print x + y

I’ve tried to do something with inspect, getting the source of the function, and then compiling it into a code object afterwards, however this resulted in a 'code' object is not callable exception or something similar.

Is this even possible? Are there an easier/better solution?

  • 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-07T07:16:10+00:00Added an answer on June 7, 2026 at 7:16 am

    There are a few different approaches to what you want to do,
    varying in “cleaninness” (as opposed to add-hoc hackish),
    easinness to do (and to understand and maintain later),
    dependency on Python implementation, and so on.

    The limitting factor I see on which approach to use depends on
    how you can/want to interfere the declaration of the decorated
    function itself.

    1. So, if you are in a position
    to change all the code that will make use of your decorator
    (or document it to others that will use it), the most straightforward,
    simpler, and whcih I think I would recomend, way to go, is to re-assign
    the function’s default argument tuple (foo.func_defaults) at run time.

    This approach requires that the variables you want to be overwritten in the running
    code be declared as function parameters with default values:

    (I will use a simpler decorator than yours for documenting purposes)

    new_args = (10,20)
    
    def process(func):
        def new_func(*args, **kw):
            func_defaults = func.func_defaults
            func.func_defaults = new_args
            result = func(*args, **kw)
            func.func_defaults = func_defaults
            return result
        return new_func
    
    @process
    def foo(x=1, y =2):
        print x + y
    

    And that “just works”(tm)

    >>> foo()
    30
    

    2. Another approach, still requiring the decorated function to follow an specific pattern,
    is to declare all variables you want to be overwritten as global variables, and at call time, create a new function object, with everything just equal the original function,
    but changing it’s globals dictionary. Since you are creating a new function object for each call, this would be thread safe -0 however, you can’t change variables that are declared as function parameters (as Python marks thos as “local”, and they can not be subsequently marked as “global”)

    from types import FunctionType
    
    new_args = {"x":10, "y":20} 
    
    def process(func):
        def new_func(*args, **kw):
            temp_func = FunctionType(func.func_code,
                                     new_args,
                                     func.func_name, 
                                     func.func_defaults,
                                     func.func_closure)
            return temp_func(*args, **kw)
        return new_func
    
    
    @process
    def foo():
        global x, y
        print x + y
    

    3. Following the same approach, for each local variable that you want overriden,
    you’d list it at the top of the function as receiving a default value – like
    in

    def foo(x=1,y=2):
       z= 3
       w = 4
    

    And at run time you would re-build dinamically at each call, not just the function object,
    but its code object as well – Yu could call types.CodeType with teh same parameters as the original func.func_code object, but for the “constants” parameter – which would replace the original’s co_consts attributes.

    I won’t give an example for this way to do things, as it falls in the domain of
    “nice experimental hack, but horrendous for actual production code” – as it is too implementation dependent.

    4. You can, on your decorator, set code tracing on – which is usually done by debuger utilities, so that you get a call back to a function you specify for each expression to be computed on the target code –
    You could them, enable the “settrace”, and in your callback function fiddle with the local variables on the target function.
    This is apratnly simple, once you see the documentation for sys.gettrace – http://docs.python.org/library/sys.html#sys.settrace — however it does hvae the small inconvenient of not working at all 🙂
    This happens because although you do have right to read and write on the frame locals dictionary, Language optimizations not always fetch values from that dictionary when using them (if ever).
    So this straight forward example shows it not working:

    >>> import sys
    >>> def a():
    ...    b = 1
    ...    f = sys._getframe()
    ...    print f.f_locals
    ...    f.f_locals["b"] = 2
    ...    print b
    ... 
    >>> a()
    {'b': 1, 'f': <frame object at 0x1038cd0>}
    1
    

    5. Finally, you could do exactly as you say, and use Python’s “dis” module and compiler functions to inject variable attribution inside your function code – directly on the bytecode. You’d have to: decompile the function’s code object, successfully meddle with the bytecode to properly load your desired variables with the wanted values, recompile teh byte code, recreate the code object (using teh before mentioned types.CodeType call), passing an appropriate “constants” value to properly load your variables, recreate teh function object from that and make your function call.

    I will not enumerate the reasons why I find this approach less useful than others, neither try a reference implementation. It would work, if properly done, though, at least for cpython 2.x.

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

Sidebar

Related Questions

This might be a bit tricky setup but... I have this Silverlight project that
This is a bit tricky to explain but I will try my best to
OK this is a bit tricky but basically in my SQL statement im comparing
I might be being a bit thicky here but please answer me this. Consider
This is a deceptively tricky little bit of HTML/JS that has either me or
So I have this bit of code for x in range(x1,x2): for y in
I am using this bit of code successfully: $(window).bind('resize',function() { window.location.href = window.location.href; });
I have this bit of code: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data;
This is getting little bit tricky. I am using state pattern in my application
This is a bit of a weird desire but I am making a matching

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.