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

  • Home
  • SEARCH
  • 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 8867383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:01:01+00:00 2026-06-14T17:01:01+00:00

Is there a possibility to create real copies of python functions? The most obvious

  • 0

Is there a possibility to create real copies of python functions? The most obvious choice was http://docs.python.org/2/library/copy.html but there I read:

It does “copy” functions and classes (shallow and deeply), by
returning the original object unchanged;

I need a real copy, because I might change some attributes of the function.

Update:

I’m aware of all the possibilities which are mentioned in the comments. My use case is based on meta programming where I construct classes out of some declarative specifications. Complete details would be too long for SO, but basically I have a function like

def do_something_usefull(self,arg):
    self.do_work()

I will add this method to various classes. Thoses classes can be completly unrelated. Using mixin classes is not an option: I will have many such functions and would end up adding a base class for each function. My current “workaround” would be to wrap this function in a “factory” like this:

def create_do_something():
    def do_something_usefull(self,arg):
        self.do_work()

That way I always get a new do_something_useful function, but I have to wrap all my functions like this.

You can trust me, that I’m aware, that this is no “normal” OO programming. I know how to solve something like that “normally”. But this is a dynamic code generator and I would like to keep everything as lightweight and simple as possible. And as python functions are quite normal objects, I don’t think it’s too strange to ask how to copy them!?

  • 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-14T17:01:02+00:00Added an answer on June 14, 2026 at 5:01 pm

    In Python3:

    import types
    import functools
    
    def copy_func(f):
        """Based on http://stackoverflow.com/a/6528148/190597 (Glenn Maynard)"""
        g = types.FunctionType(f.__code__, f.__globals__, name=f.__name__,
                               argdefs=f.__defaults__,
                               closure=f.__closure__)
        g = functools.update_wrapper(g, f)
        g.__kwdefaults__ = f.__kwdefaults__
        return g
    
    def f(arg1, arg2, arg3, kwarg1="FOO", *args, kwarg2="BAR", kwarg3="BAZ"):
        return (arg1, arg2, arg3, args, kwarg1, kwarg2, kwarg3)
    f.cache = [1,2,3]
    g = copy_func(f)
    
    print(f(1,2,3,4,5))
    print(g(1,2,3,4,5))
    print(g.cache)
    assert f is not g
    

    yields

    (1, 2, 3, (5,), 4, 'BAR', 'BAZ')
    (1, 2, 3, (5,), 4, 'BAR', 'BAZ')
    [1, 2, 3]
    

    In Python2:

    import types
    import functools
    def copy_func(f):
        """Based on http://stackoverflow.com/a/6528148/190597 (Glenn Maynard)"""
        g = types.FunctionType(f.func_code, f.func_globals, name=f.func_name,
                               argdefs=f.func_defaults,
                               closure=f.func_closure)
        g = functools.update_wrapper(g, f)
        return g
    
    def f(x, y=2):
        return x,y
    f.cache = [1,2,3]
    g = copy_func(f)
    
    print(f(1))
    print(g(1))
    print(g.cache)
    assert f is not g
    

    yields

    (1, 2)
    (1, 2)
    [1, 2, 3]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would know if there is the possibility to create a new Android Activity
Is there possibility to compile windows service using only mingw c++ compiler and library?
I was wondering, is there any possibility to create a table where i have
I wonder if there is a (native) possibility to create a MySQL table from
As I see in documentation there is a possibility to create gist index which
Is there any possibility to create CSS definition for any element with the class
is there any possibility to create a server based on java that connects to
I ask the question more specific: Using Netbeans, is there a possibility to create
Is there any possibility to create a listener for closing another process? Let's say
In JavaScript there is the possibility to create getters and setters the following way:

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.