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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:27:11+00:00 2026-05-23T10:27:11+00:00

I have a function that I want to use to manipulate an input parameter,

  • 0

I have a function that I want to use to manipulate an input parameter, however, in the process I must copy the input parameter. I do not want to return the modified object (because multiprocessing may or may not be involved). How can I reassign the input parameter to the copied value? Here’s a representation of the function:

from copy import deepcopy, copy

def myFunc(myObj,i):
    myObj.x = 'start'
    # I can't avoid this step:
    modObj = deepcopy(myObj)
    modObj.x = 'modified'
    if i == 0:
        myObj.x = modObj.x
    elif i == 1:
        myObj = modObj
    elif i == 2:
        myObj = copy(modObj)
    elif i == 3:
        myObj.__dict__ = modObj.__dict__

class MyClass(object):
   def __init__(self):
       self.x = "construct"

for i in range(4):
   temp = MyClass()
   myFunc(temp,i)
   print i,temp.x

And here’s the output:

0 modified
1 start
2 start
3 modified

Since option #3 is so close to what I want to do, is there an “official” way to do it? E.g.

myObj.shallowcopy(modObj)

where this shallowcopy behaves similarly to the shallow copy in copy. One alternative would be to embed MyClass as a member of some other class, and pass that class to the function… but if I can do that, why can’t python just save me that step by copying over member data? I’ve read this, but they didn’t seem to come up with an answer for me.

  • 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-23T10:27:12+00:00Added an answer on May 23, 2026 at 10:27 am

    Update: Ok, now that I understand more about the problem, I would suggest a combination of options 1 and 4: create public update() and dictify() methods. These could be wrappers around option 4 or some variation like myObj.__dict__.update(modObj.__dict__) — both of which are somewhat hackish.

    Or, they could be elegant, readable, self-documenting methods that accept and return (respectively) the appropriate dictionary representation of self, which may or may not coincide with self.__dict__. I think the reason Python doesn’t have any built-in functionality for things like this is that there’s no truly general way to implement it, because not all objects have a __dict__. This kind of behavior has to have a customized implementation.

    If manipulating __dict__ is sufficient for your needs, fine, but you save yourself some trouble if you hide that manipulation behind an abstraction that can be altered later, if you decide you want to implement __slots__ or something like that.

    (Leaving the below for historical purposes)


    This confuses me: “I do not want to return the modified object (because multiprocessing may or may not be involved).” If you mean the multiprocessing module, then even options 0 and 3 will fail, because processes don’t share memory.

    >>> for i in range(4):
    ...     temp = MyClass()
    ...     p = Process(target=myFunc, args=(temp, i))
    ...     p.start()
    ...     p.join()
    ...     print i,temp.x
    ... 
    0 construct
    1 construct
    2 construct
    3 construct
    

    See? If you want to transfer data between processes, you have to use one of multiprocessing‘s built-in types.

    >>> from multiprocessing import Process, Queue
    >>> 
    >>> def myFuncQ(myObj, i, return_queue):
    ...     myObj.x = 'start'
    ...     modObj = deepcopy(myObj)
    ...     modObj.x = 'modified'
    ...     return_queue.put(modObj)
    ... 
    >>> return_queue = Queue()
    >>> for i in range(4):
    ...     temp = MyClass()
    ...     p = Process(target=myFuncQ, args=(temp, i, return_queue))
    ...     p.start()
    ...     p.join()
    ...     temp = return_queue.get()
    ...     print i,temp.x
    ... 
    0 modified
    1 modified
    2 modified
    3 modified
    

    If you aren’t using multiprocessing, then a Queue will serve as well as any other mutable container for this purpose. That’s the closest there is to an “official” way to pass by reference in Python.

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

Sidebar

Related Questions

I have a function (for ease, I'll just use count()) that I want to
I want to have a function that will return the reverse of a list
I have a function that send a JSONResult, Now i want to use that
I have a simple function that I want to call in the code behind
I have a function that is effectively a replacement for print, and I want
I have a function, a() , that I want to override, but also have
I have a base class Foo that has an Update() function, which I want
I have a function that takes, amongst others, a parameter declared as int privateCount
I want to make sure that a set of functions have the same signature
I have a function that gives me the following warning: [DCC Warning] filename.pas(6939): W1035

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.