2

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)

Carson Myers
  • 37,678
  • 39
  • 126
  • 176
  • 2
    You can mutate objects in-place in functions, but what you're doing here just reassigns the name `destination` within your function; it doesn't affect `some_dict` at all. Python passes around objects. It doesn't have the same pass-by-reference semantics as, say, C++. – Eevee May 28 '11 at 06:18
  • hmm, weird, if I pass the whole dict to the function it changes it, but if I pass a value from it it doesn't. Lemme change the question – Carson Myers May 28 '11 at 06:25
  • Right; assigning to a key changes the dict, but assigning to a name reuses it without touching the original value. – Eevee May 28 '11 at 06:29
  • ah, there, I oversimplified -- forgot that a string in a dict is still immutable ;) for real it's a big soap object full of soap objects, and they're all mutable -- and I pass each of them through these formatting functions, and they do wind up mutated – Carson Myers May 28 '11 at 06:29
  • @Eevee really? I thought it had more to do with the mutability of whatever you pass in: if I pass `foo["bar"]` into a function that will mutate it, and `foo["bar"]` points to a list, then the changed list will show up in the original dict, no? – Carson Myers May 28 '11 at 06:33
  • It's only about mutating the object in-place, as contrasted with reassigning just the variable name. If you *change the list*, yes. If you assign a new value to the variable that contains the list inside your function, then no. Mutability only matters because you just can't do the former with immutable values. But e.g. `def foo(x): x = [0, 0, 0]` `a = [1, 2, 3]; foo(a)` won't do anything. – Eevee May 28 '11 at 06:40

1 Answers1

1

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.

Eevee
  • 47,412
  • 11
  • 95
  • 127