|

楼主 |
发表于 2021-2-21 14:40:48
|
显示全部楼层
本帖最后由 Python初学者8号 于 2021-2-21 19:18 编辑
关于昨天的问题的解答:
- #!/usr/bin/env python
- # coding: utf-8
- # In[2]:
- test = list(range(6))
- print('test is',test)
- # In[3]:
- for t in test:
- print('t=%s,id(%s)=%s '%(t,t,id(t)))
- # In[4]:
- for i in range(6):
- print('test[i]=%s,id(%s)=%s '%(test[i],test[i],id(test[i])))
- # In[5]:
- for t in test:
- t = t**2
- print('t=%s,id(%s)=%s '%(t,t,id(t)))
- # In[6]:
- for i in range(6):
- test[i] = test[i]**2
- print('test[i]=%s,id(%s)=%s '%(test[i],test[i],id(test[i])))
- # In[8]:
- x = 6
- print(id(x))
- # In[9]:
- def f():
- x = 6
- print(id(x))
- f()
-
- # In[17]:
- def f():
- x = 6
- print(id(x))
- f()
-
- # In[15]:
- print(id(6))
复制代码
可能是这样
1.for其实也是一种函数,它所提供的的i一个局部变量,然后i从哪里取值呢?是从in 后年的可迭代传入值。并且要牢记python内存机制中关于变量赋值的‘标签’作用
2.id(object)只是找到对象的内存,和标签无关,标签只是指引这个对象而已
|
|