请高手看看这段python代码中赋值引用问题是什么问题,python赋值,关于python引用的问
请高手看看这段python代码中赋值引用问题是什么问题,python赋值,关于python引用的问
关于python引用的问题,有p的是我要的结果。
有p这个中间变量后为什么结果不同,tire和p是怎么引用的,
有图最好了。
代码:
def no_p(): trie = {} word = 'foot' print('no_p', id(trie)) for c in word: if c not in trie: trie[c] = {} print('no_p:', c, trie) print('no_p', id(trie)) trie = trie[c] print('no_p:', id(trie)) trie[''] = '' print('no_p:', trie)def have_p(): trie = {} p = trie word = 'foot' print('have_p:', id(trie), id(p)) for c in word: if c not in p: p[c] = {} print('have_p:', c, trie) print('have_p:', id(trie), id(p)) p = p[c] print('have_p:', id(trie), id(p)) p[''] = '' print('have_p:', trie)if __name__ == '__main__': no_p() have_p()
结果:
no_p 21667928
no_p: f {'f': {}}
no_p 21667928
no_p: 21680536
no_p: o {'o': {}}
no_p 21680536
no_p: 21667928
no_p: o {'o': {}}
no_p 21667928
no_p: 21680536
no_p: t {'t': {}}
no_p 21680536
no_p: 21667928
no_p: {'': ''}
have_p: 21667928 21667928
have_p: f {'f': {}}
have_p: 21667928 21667928
have_p: 21667928 21680536
have_p: o {'f': {'o': {}}}
have_p: 21667928 21680536
have_p: 21667928 21680336
have_p: o {'f': {'o': {'o': {}}}}
have_p: 21667928 21680336
have_p: 21667928 44964360
have_p: t {'f': {'o': {'o': {'t': {}}}}}
have_p: 21667928 44964360
have_p: 21667928 44964760
have_p: {'f': {'o': {'o': {'t': {'': ''}}}}}
Django 官方文档的 Tutorial 就是一个简单的投票系统——如果您指的“app”是一个 web 应用的话:
首先说明一下,由于个人的洁癖问题,我不认为Python里存在“变量”这种东西,存在的只是“名字”(name)。
在你的no_p的代码里,第一次trie = trie[c]之后,trie这个名字现在指向trie[c]所指向的那个对象,同时你也丢失了对trie原来指向的那个字典的引用(而且由于现在没有任何名字指向它,你基本上已经不可能获得对它的引用了,它甚至可能会被GC掉)。代码这样跑下去,你的trie只会指向叶子节点(还是其它什么东西)。
初始状态 +------+ trie = trie[c]后 +------+ +-------+ | root | | root | --> | child | +------+ +------+ +-------+ ^ ^ ^ | | | +- trie +- 丢失引用 +- trie
所以现在可以得出结论,就是你的no_p缺少对整个树的引用,这也是你在have_p里实现了的东西。
初始状态 +------+ p = p[c]后 +------+ +-------+ | root | | root | --> | child | +------+ +------+ +-------+ ^ ^ ^ ^ / \ | | p -+ +- trie +- trie +- p
不知道我说的够不够清楚。
编橙之家文章,
相关内容
- 文中源码的线程和进程有本质区别是什么?傻傻分不清
- Python装饰器这种用法可以吗,Python装饰器用法,class Ev
- Python sdk文档中尖括号如何处理,pythonsdk文档括号,比如
- 为什么读取通过cookielib方法得到的cookies返回值是空的
- 请教Python分布式实现一个helloworld的思路,pythonhellowor
- Python syslog模块怎么判断发送状态,pythonsyslog,syslog服务
- Python class Vector 的创建,pythonvector,创建一个向量的cla
- Python 源码中的re表达式同原生字符是怎么匹配成功的,
- Mac python2.7.3创建虚拟环境提示ImportError: No module named .
- PythonGIL与线程安全问题?,pythongil安全问题,由于一些历
评论关闭