ResMar ResMar - 1 year ago 73
Python Question

Seemingly immutable dict in object instance

If I run the following


import as ccrs
globe = ccrs.LambertCylindrical()
globe.proj4_params['a'] = 5

I get:

{'proj': 'cea', 'lon_0': 0.0, 'a': 57.29577951308232, 'ellps': 'WGS84'}
{'proj': 'cea', 'lon_0': 0.0, 'a': 57.29577951308232, 'ellps': 'WGS84'}

Impying that the
property is immutable.

But it's just a bog standard


<class 'dict'>

Which, since it's implemented in C, can't be overwritten to have this kind of behavior (at least not safely).

Ok, but the code for this class is dead simple, so there's something wrong with my understanding. Can someone explain to me why I am getting this behavior?


The following:

projection.proj4_params = dict(projection.proj4_params)

Results in:

*** AttributeError: attribute 'proj4_params' of 'cartopy._crs.CRS' objects is not writable

Answer Source

You could ensure your params are a dictionary by converting it to one first.

params = dict(globe.proj4_params)

Then if you print

print type(params)

<type 'dict'>

Notice the <type 'dict'> instead of the <class 'dict'> in your question. The dict you're using may be a custom class that's immutable but uses the same dict name.