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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:20:42+00:00 2026-05-13T12:20:42+00:00

I want to execute a method with a copy of the original self passed

  • 0

I want to execute a method with a copy of the original self passed while execution.

Here is the code I’m talking about:

def protect_self(func):
    from copy import copy
    from functools import wraps
    @wraps(func)
    def decorated(self, *args, **kwargs):
        self_copy = copy(self)
        return func(self_copy, *args, **kwargs)
    return decorated

In my understanding the copy function creates a new object of the same type and
copies the __dict__ of the old one to the new object (using references, so
changes to actual object instances in __dict__ will still affect the original object).

Does this mean I can be sure that the decorated method cannot modify __dict__ of
the original instance?

Just to make sure: I don’t need a secure sandbox behaviour. My purpose is just
to have a single object instanciated which I will use as a factory. The
protected method should be possible to modify the passed self but it should
be reseted afterwards.

  • 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-13T12:20:42+00:00Added an answer on May 13, 2026 at 12:20 pm

    As the OP clarified in a comment that the purpose is to be threadsafe, then there’s an obvious issue — copy.copy itself isn’t threadsafe, in addition to the issue already pointed out, that copy.copy makes a shallow copy and so (while self.__dict__ itself won’t be modified) mutable objects can perfectly well get altered. Using copy.deepcopy deals with this (at a potentially hefty price in terms of performance) but in a sense even worsens the issue of thread-safety (since deep-copying can take so much longer than shallow-copying, the risk of a race condition actually occurring grows by leaps and bounds — not that I’m in any way, shape or form recommending having race conditions that occur “only rarely”, mind!-).

    If you have to make originally-unsafe methods thread-safe, you’ll have to bite the bullet and use locks (or a Queue and an auxiliary thread to serialize the operations) — I guess that if you further need to silently ignore the methods’ attempts to alter objects, you’ll moreover have to deepcopy everything (why stop at self — what if those methods were altering globals, for example?!-). Seem a very iffy proposition to me.

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

Sidebar

Related Questions

I want to execute a javascript function in a c# code. Here is what
I'm testing a page with Selenium IDE and want to execute a method already
I have asp.net generated html code. I want execute jQuery click function when user
I want to execute a code helloword.cpp which takes in some argument from console
I want execute below callback() method after completion of document.getElementById('btnDownload').click(); method . Now callback()
I want to execute a method on VB.Net to return a date which is
I want to execute the method uploadingDone() after completion of all three threads. The
I only want to execute a method call when the user clicks on a
I've the following model and I want to execute a method on save and
I want to execute a static method from certain class in certain namespace, but

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.