在Python中获取内存地址,确定它是列表还是字典并更新其内容 [英] Get memory address in Python, identify if it is list or a dictionary and update its content

查看:316
本文介绍了在Python中获取内存地址,确定它是列表还是字典并更新其内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

alist += [4]alist = alist + [4]不同,因为前者更改了alist的引用,而后者则没有.我在下面的IDLE中使用id()进行了尝试,看来这是正确的声明.

alist += [4] and alist = alist + [4] are different as the first changes the reference of alist whereas the latter doesn't. I tried this below on IDLE by using id() and it seems like it is correct claim.

在IDLE(Python 3.6.1)上执行的代码

Code executed on IDLE (Python 3.6.1)

>>> alist = [1, 2, 3]
>>> id(alist)
50683952
>>> alist += [4]
>>> id(alist)
50683952
>>> alist = alist + [4]
>>> id(alist)
50533080

以下是id()的文档: https://docs.python .org/3/library/functions.html#id

是否可以通过编程方式获取对内存地址的引用,识别该位置是否具有列表或字典,读取内容并使用引用来更新内容?

Is it possible to get reference to memory address programatically, identify whether the location has a list or a dictionary, read the contents and update the content using reference?

NOTE:我找到了2条相关的stackoverflow帖子,但不确定它们是否回答了我的问题.

NOTE: I found 2 relevant stackoverflow posts but I am not sure if they answer my question.

  • print memory address of Python variable
  • Accessing object memory address

推荐答案

您正在尝试使用 Dereference运算符(例如*)在C语言中执行类似的操作,您可以在中阅读更多内容此问题. 我确信Python不支持您要实现的目标,您必须改变主意范式,以不同的方式获得所需的结果.

You are trying to do something like in C, with a Dereference operator, such as * , you can read more about it in this question. I'm sure that Python has no support for doing what you are trying to achieve, you have to change your mind paradigm, think different to get the results you want.

阅读时,您可以从对象中获取内存地址" ,但是您想找到一种方法(至少在python 2-3中)找回方法

As you read, you can "get the memory address" from an object, but you want ever find a way (at least in python 2 - 3) to get the way back

这篇关于在Python中获取内存地址,确定它是列表还是字典并更新其内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆