`
zsjg13
  • 浏览: 145172 次
  • 性别: Icon_minigender_1
  • 来自: 安徽
社区版块
存档分类
最新评论

Understanding References

阅读更多

    Python将数据放在对象中,变量只不过是对象的一个引用;变量为内存中某个点取了一个名字而已。所有对象都有一个唯一的身份号、类型以及值。

 

1. Object identity

    因为是对象,而不是变量,有一个数据类型,所以一个变量在某个时候可以引用一个list,而在另一个时候可以引用一个浮点数。一个对象它的类型永远无法改变,但对于lists以及其他一些mutable类型,它们的值是可以改变的。

    Python提供了 id(obj) 这样一个函数,它返回的就是对象的identity,也就是对象在内存中的地址。

>>> shoppingList = [‘candy’,’cookies’,’ice cream’]

>>> id(shoppingList)

17611492

>>> id(5)

3114676

    操作符 is 作用是比较两个对象的identity,判断它们是否是同一个对象。

>>> junkFood = shoppingList # Both reference the same object

>>> junkFood is shoppingList

1

>>> yummyStuff = [‘candy’,’cookies’,’ice cream’]

>>> junkFood is not yummyStuff # Different identity, but...

1

>>> junkFood == yummyStuff # ...same value

1

    因为变量仅仅是引用某个对象,修改了某个mutable对象的值后,所有引用该对象的变量都可以看到该变化:

>>> a = [1,2,3,4]

>>> b = a

>>> a[2] = 5

>>> b

[1, 2, 5, 4]

>>> a = 6

>>> b = a     # Reference the same object for now.

>>> b

6

>>> a = a + 1 # Python creates a new object to hold (a+1)

>>> b         # so b still references the original object.

6

 

2. Counting references

    每个对象都有一个引用计数,指明有多少变量在引用该对象。当你将一个变量附到一个对象上,或者当把一个对象放到一个list或其他容器中时,引用计数就会增加。当销毁、reassign、或从容器中移除一个对象,引用计数就会减少。当引用计数到达 0,Python的垃圾收集器就会销毁该对象,重新开垦这块被使用的内存。

    sys.getrefcount(obj) 函数返回指定对象的引用计数。

 

====新特性

从2.0版开始,Python现在还收集只带循环引用的对象。例如,

a = []; b = []

a.append(b); b.append(a)

a = 5; b = 10 # Reassign both variables to different objects.

The two list objects still have a reference count of 1 because each is a member of

the other’s list. Python now recognizes such cases and reclaims the memory used

by the list objects.

========================================================================================

    要记住,del语句删除的是变量,而不是对象,尽管如果一个你要删除的变量是一个对象的唯一引用,随后Python也会删除该对象。

>>> a = [1,2,3]

>>> b = a # List object has 2 references now

>>> del a # Back to 1 reference

>>> b

[1, 2, 3]

 

你也可以创建 weak references,或者那些不会影响一个对象的引用计数的引用。

分享到:
评论

相关推荐

    The-Complete-Guide-to-Understanding-Swift-Optionals pdf

    2. 不拥有引用(Unowned References):当一个变量永远不会为nil时,可以声明为unowned来避免循环引用和内存泄漏。 3. IBOutlets的初始化:在使用Interface Builder时,可以通过强制解包的方式来连接界面元素和代码...

    Big.Data.and.IT.Professionals.1514870622

    Big Data and IT Professionals: A Study of the Perceptions Held by IT Professionals in Relation to the Maturity, Value, and Practical Deployment of Big Data ...Appendix A: References Appendix B: Survey

    Design For Reliability

    2. Understanding Customer Requirements 2.1 Introduction 2.2 Specified and Unspecified Requirements 2.3 Cost of Reliability 2.4 Benchmarking 2.5 Using Failure Modes and Effects Analysis to Meet ...

    CiscoPress:802.11 Wireless Network Site Surveying and Installation.chm

    Part I: Understanding the Prerequisites of a Site Survey Chapter 1. Defining a Wireless Network's Protocols and Components The Evolution of Wireless Standards Introducing 802.11 Additional ...

    Addison.Wesley.Understanding.SCA.Service.Component.Architecture.Jun.2009.rar

    5. **引用(References)**:组件需要使用的外部服务,由其他组件提供。 6. **域(Domains)**:SCA的部署和管理单位,包含了组成应用程序的所有组件和服务。 7. **组合(Composition)**:SCA允许通过声明式方式...

    Selected.Topics.in.Cplusplus.15117

    It has pointers and references. You need to know how and when you should use them. Then you have virtual functions and virtual tables and virtual pointers. When you know these core concepts C++ ...

    Fuzzy Control Systems

    References Chapter 2—Towards a Unified Theory of Intelligent Autonomous Control Systems 1 Introduction 2 State of the Art and Future Challenges 2.1 Three views of what are the most ...

    Hibernate Reference Documentation3.1

    19.5. Understanding Collection performance 19.5.1. Taxonomy 19.5.2. Lists, maps, idbags and sets are the most efficient collections to update 19.5.3. Bags and lists are the most efficient inverse ...

    Microsoft Visual C# 2010 Step by Step Mar 2010

    8 Understanding Values and References 151 9 Creating Value Types with Enumerations and Structures 173 10 Using Arrays and Collections 191 11 Understanding Parameter Arrays 219 12 Working with ...

    Digital Color Imaging handbook

    This is intended to facilitate the understanding of digital color imaging from a systems perspective, which is becoming increasingly important in today’s open, interconnected world. Additional ...

    面向对象程序设计实验指导书实.doc

    * understanding the concept of object references * understanding the use of objects as function parameters 二、实验内容 * 验证及认知实验 + 构造函数和析构函数的定义和调用 + this 指针的使用和应用 +...

    Chandler, L. A. (1985). Children under stress: Understanding emotional adjustment reactions (2nd ed.). Springfield, IL: Thomas. 124 pp., [dollar]20.75

    Children under stress: Understanding emotional adjustment reactions (2nd ed.). Springfield, IL: Thomas. 124 pp., [dollar]20.75 102 Book Reviews REFERENCES FEURSTEIN, R . (1979). GREDLER, G. R....

    Printed Circuits Handbook

    References / 2.21 Chapter 3. Semiconductor Packaging Technology 3.1 3.1 Introduction / 3.1 3.2 Single-Chip Packaging / 3.5 3.3 Multichip Packages / 3.15 3.4 Optical Interconnects / 3.18 3.5 High-...

    Python Testing Cookbook (第1版)

    Each recipe contains explanations of how it works along with answers to common questions and cross references to other relevant recipes. The easy-to-understand recipe names make this a handy test ...

    Visual C++ 6.0 21天自学教程

    References Classes Constructors and Destructors Inheritance Summary -------------------------------------------------------------------------------- The appendix is designed to provide you ...

    Game Engine Architecture

    The references and citations make it a great jumping off point for those who wish to dig deeper into any particular aspect of the game development process. Intended as the text for a college level ...

Global site tag (gtag.js) - Google Analytics