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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:59:16+00:00 2026-05-22T18:59:16+00:00

I’m working in Python here (which is actually pass-by-name, I think), but the idea

  • 0

I’m working in Python here (which is actually pass-by-name, I think), but the idea is language-agnostic as long as method parameters behave similarly:

If I have a function like this:

def changefoo(source, destination):
    destination["foo"] = source
    return destination

and call it like so,

some_dict = {"foo": "bar"}
some_var = "a"

new_dict = changefoo(some_var, some_dict)

new_dict will be a modified version of some_dict, but some_dict will also be modified.

Assuming the mutable structure like the dict in my example will almost always be similarly small, and performance is not an issue (in application, I’m taking abstract objects and changing into SOAP requests for different services, where the SOAP request will take an order of magnitude longer than reformatting the data for each service), is this okay?

The destination in these functions (there are several, it’s not just a utility function like in my example) will always be mutable, but I like to be explicit: the return value of a function represents the outcome of a deterministic computation on the parameters you passed in. I don’t like using out parameters but there’s not really a way around this in Python when passing mutable structures to a function. A couple options I’ve mulled over:

  • Copying the parameters that will be mutated, to preserve the original

    I’d have to copy the parameters in every function where I mutate them, which seems cumbersome and like I’m just duplicating a lot. Plus I don’t think I’ll ever actually need the original, it just seems messy to return a reference to the mutated object I already had.

  • Just use it as an in/out parameter

    I don’t like this, it’s not immediately obvious what the function is doing, and I think it’s ugly.

  • Create a decorator which will automatically copy the parameters

    Seems like overkill

So is what I’m doing okay? I feel like I’m hiding something, and a future programmer might think the original object is preserved based on the way I’m calling the functions (grabbing its result rather than relying on the fact that it mutates the original). But I also feel like any of the alternatives will be messy. Is there a more preferred way? Note that it’s not really an option to add a mutator-style method to the class representing the abstract data due to the way the software works (I would have to add a method to translate that data structure into the corresponding SOAP structure for every service we send that data off too–currently the translation logic is in a separate package for each service)

  • 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-22T18:59:17+00:00Added an answer on May 22, 2026 at 6:59 pm

    If you have a lot of functions like this, I think your best bet is to write a little class that wraps the dict and modifies it in-place:

    class DictMunger(object):
        def __init__(self, original_dict):
            self.original_dict = original_dict
    
        def changefoo(source)
            self.original_dict['foo'] = source
    
    some_dict = {"foo": "bar"}
    some_var = "a"
    
    munger = DictMunger(some_dict)
    munger.changefoo(some_var)
    # ...
    new_dict = munger.original_dict
    

    Objects modifying themselves is generally expected and reads well.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want to count how many characters a certain string has in PHP, but
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I have a text area in my form which accepts all possible characters from
I want use html5's new tag to play a wav file (currently only supported
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,

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.