Comments (3)
In [13]: a = [9, 7, 5, 3, 1]
In [14]: b = a
In [15]: b
Out[15]: [9, 7, 5, 3, 1]
In [16]: a.reverse()
In [17]: a
Out[17]: [1, 3, 5, 7, 9]
In [18]: b
Out[18]: [1, 3, 5, 7, 9]
上面的a和b其实是指向的同一个内存地址,修改任一个的值,都会影响到另一个。
In [26]: a.__str__
Out[26]: <method-wrapper '__str__' of list object at 0x7fc1f7f68988>
In [27]: b.__str__
Out[27]: <method-wrapper '__str__' of list object at 0x7fc1f7f68988>
下面是n是m的拷贝,m、n内存地址不同。
In [24]: m.__str__
Out[24]: <method-wrapper '__str__' of list object at 0x7fc1f7f6fdc8>
In [25]: n.__str__
Out[25]: <method-wrapper '__str__' of list object at 0x7fc1f7e6bdc8>
In [19]: m = [0, 2, 4, 6, 8]
In [20]: n = m.copy()
In [21]: m.reverse()
In [22]: m
Out[22]: [8, 6, 4, 2, 0]
In [23]: n
Out[23]: [0, 2, 4, 6, 8]
再深一点就是__深拷贝和浅拷贝__了
个人愚见。
from starterlearningpython.
有道理。很多高级语言,都是将某些基本类型和由基本类型组合的类型,以不同的存储方式处理。
2016-05-05 17:04 GMT+08:00 lambdaplus [email protected]:
In [13]: a = [9, 7, 5, 3, 1]
In [14]: b = a
In [15]: b
Out[15]: [9, 7, 5, 3, 1]In [16]: a.reverse()
In [17]: a
Out[17]: [1, 3, 5, 7, 9]In [18]: b
Out[18]: [1, 3, 5, 7, 9]上面的a和b其实是指向的_同一个内存地址_,修改任一个的值,都会影响到另一个。
In [26]: a.str
Out[26]: <method-wrapper 'str' of list object at 0x7fc1f7f68988>In [27]: b.str
Out[27]: <method-wrapper 'str' of list object at 0x7fc1f7f68988>
下面是n是m的拷贝,m、n内存地址不同。
In [24]: m.str
Out[24]: <method-wrapper 'str' of list object at 0x7fc1f7f6fdc8>In [25]: n.str
Out[25]: <method-wrapper 'str' of list object at 0x7fc1f7e6bdc8>In [19]: m = [0, 2, 4, 6, 8]
In [20]: n = m.copy()
In [21]: m.reverse()
In [22]: m
Out[22]: [8, 6, 4, 2, 0]In [23]: n
Out[23]: [0, 2, 4, 6, 8]再深一点就是__深拷贝和浅拷贝__了
个人愚见。—
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub
#98 (comment)
QiWei
from starterlearningpython.
谢谢!
from starterlearningpython.
Related Issues (20)
- 关于 独立性 是否可以多说一点? 208.md HOT 1
- 关于 多重继承 209.md
- 209.md 调用覆盖方法 小节中的表述问题。 HOT 1
- 109.md 问题 HOT 1
- 113.md 问题 HOT 1
- 请教老师集合的问题 HOT 1
- 书籍gitbook版地址 HOT 1
- French Navy last morse code message HOT 1
- 228.md(第三方库)图片显示问题。 HOT 1
- 105.md 错别字修正 HOT 2
- 109.md-错别字纠正 即第一个参数传入的"pytohn" HOT 3
- 建议把 StarterLearningPython/ 目录下的md文件集中到一个子目录下 HOT 1
- 字符串(4) - .format() HOT 1
- 大学如果开设齐老师这门课就好了 HOT 1
- 跟老齐学Python 这个例子怎么运行不对? HOT 3
- 文章404 HOT 3
- Learning sequence HOT 1
- 211.md中的问题 HOT 1
- 如何批量读取多个.xlsx数据文件? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from starterlearningpython.