0

In my specific case, I would like to pass a dictionary that contains lists that I don't want exposed without having to make a time-consuming deep copy. But, I am also wondering this just in general as well.

In my case I am trying to pass an object to this one:

dictionary = 
{'x': 
    {
    0: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
    1: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
    2: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
    3: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
}, 
    'y':
{
    0: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
    1: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
    2: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
    3: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
} 
.
.
.
stuffbyax
  • 31
  • 4
  • is it an option to use a tuple instead of a list? – Paul H Apr 06 '21 at 23:38
  • I guess I could cast all the lists as tuples, but this particular dictionary is converted from a very large Pandas DataFrame. I'm wondering if there is a faster / simpler / more pythonic way to do it. – stuffbyax Apr 06 '21 at 23:43
  • i need to see some code demonstrating what's going on. I'm not following you – Paul H Apr 06 '21 at 23:45
  • 1
    You can't make things magically immutable, no. – Mad Physicist Apr 06 '21 at 23:50
  • I feel like you'll be better off keeping the data in the dataframe, but it's hard to know what you're actually trying to do. This feels like a classic X-Y problem – Paul H Apr 06 '21 at 23:51
  • "_I would like to pass a dictionary_" -- pass the dictionary to what? – Paul H Apr 06 '21 at 23:52
  • I'm trying to pass a dictionary JSON style to another program running in a separate thread. I've also added an example. – stuffbyax Apr 06 '21 at 23:54
  • 1
    If its in another separate application/thread, how is mutability an issue? – Paul H Apr 06 '21 at 23:57
  • Some of the insights in https://stackoverflow.com/questions/4828080/how-to-make-an-immutable-object-in-python might be useful. – Paul Whipp Apr 06 '21 at 23:57
  • @PaulH if I pass this dictionary to another thread, and say they edit one of the lists, (eg. `dict['y'][0] = 1`) it'll change the list everywhere no? – stuffbyax Apr 07 '21 at 00:02
  • I thought it was another program in another thread (e.g., something outside the python executable) – Paul H Apr 07 '21 at 01:07

1 Answers1

2

without converting to an immutable type (for example list to tuple) you can't magically convert an object to be immutable.

Immutability is not a flag or something simple like that - it is about the behaviour of the methods on the object.

You could create your own custom container that has a mutable toggle (and which effectively disables some methods when mutable is on), but Python doesn't offer anything like that out of the box.

Tony Suffolk 66
  • 9,358
  • 3
  • 30
  • 33
  • ok, I thought as much. I guess I was wondering if there was some library that included some container that preserved the state of any object while keeping it observable. Thanks! – stuffbyax Apr 06 '21 at 23:59
  • 1
    @stuffbyax no, I wish the standard library provided efficient, immutable wrappers, e.g. `ImmutableList(some_regular_list)` which would just serve as an immutable view over another list, or say, `ImmutableMapping(some_dict)`, but alas... You can make one yourself, though – juanpa.arrivillaga Apr 07 '21 at 00:05
  • @juanpa.arrivillaga That's too bad. It would be very useful – stuffbyax Apr 07 '21 at 00:08
  • There is an immuatble list - it is called a tuple - there is also an immutable set - called frozenset: but to use both you have to do a shallow copy. A lockable object is a different prospect that doesn't exist, and would create some interesting semantics. – Tony Suffolk 66 Apr 07 '21 at 12:28