How to reference a dict object?

2024/9/20 9:01:49

I have a Python dict object d. d = {'a': 1, 'b': 2, 'c': 3}. My problem is really simple. I want to reference a variable to the elements of d. For example, something like:

In[1]: p = d['a']
>>> p = 1
In[2]: p = 2
In[3]: d
>>> d = {'a': 2, 'b': 2, 'c': 3}

Is that even possible? But as far as I know dict objects are mutable.

Answer

No, this is not possible. After executing the line

p = d['a']

The situation does not look like this:

p  ───>  d['a']  ───>  1

Rather, it looks like this:

 p  ───>  1^│
d['a'] ───┘

The name p is bound directly to whatever object was resolved as the value for key 'a'. The variable p knows nothing about the dict d, and you could even delete the dict d now.

https://en.xdnf.cn/q/72519.html

Related Q&A

Python 3.3: DeprecationWarning when using nose.tools.assert_equals

I am using nosetest tools for asserting a python unittest:... from nose.tools import assert_equals, assert_almost_equalclass TestPolycircles(unittest.TestCase):def setUp(self):self.latitude = 32.074322…

Panel/Hvplot interaction when variable is changing

Im trying to create a dashboard with two holoviews objects: a panel pn.widgets.Select object that contains a list of xarray variables, and a hvplot object that takes the selected variable on input, lik…

asyncio matplotlib show() still freezes program

I wish to run a simulation while at the same time output its progress in a plot. Ive been looking through a lot of examples of threading and multiprocessing, but they are all pretty complex. So I thoug…

celery .delay hangs (recent, not an auth problem)

I am running Celery 2.2.4/djCelery 2.2.4, using RabbitMQ 2.1.1 as a backend. I recently brought online two new celery servers -- I had been running 2 workers across two machines with a total of ~18 thr…

Python ttk.combobox force post/open

I am trying to extend the ttk combobox class to allow autosuggestion. the code I have far works well, but I would like to get it to show the dropdown once some text has been entered without removing fo…

How to pull from the remote using dulwich?

How to do something like git pull in python dulwich library.

convert python programme to windows executable

i m trying to create windows executable from python program which has GUI . i m using following scriptfrom distutils.core import setup import py2exesetup(console=[gui.py]) it gives following errorWarni…

Must explicitly set engine if not passing in buffer or path for io in Panda

When running the following Python Panda code:xl = pd.ExcelFile(dataFileUrl)sheets = xl.sheet_namesdata = xl.parse(sheets[0])colheaders = list(data)I receive the ValueError:Must ex…

How to access the keys or values of Python GDB Value

I have a struct in GDB and want to run a script which examines this struct. In Python GDB you can easily access the struct via(gdb) python mystruct = gdb.parse_and_eval("mystruct")Now I got t…

How can I fire a Traits static event notification on a List?

I am working through the traits presentation from PyCon 2010. At about 2:30:45 the presenter starts covering trait event notifications, which allow (among other things) the ability to automatically ca…