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,或者那些不会影响一个对象的引用计数的引用。
相关推荐
Chapter 1: Understanding and Meaning 1 Understanding 1 Meaning 13 Chapter 2: Components and Conditions of an Act of Understanding 27 What Could Be an Act of Understanding?...References 170 Index 183
Simplified methods to understand complicated concepts such as spectral estimation and switched noise References that lead to in-depth analysis of specialized topics Figures and charts that illustrate...
viii SIP: Understanding the Session Initiation Protocol 4 SIP Request Messages 71 4.1 Methods 71 4.1.1 INVITE 72 4.1.2 REGISTER 74 4.1.3 BYE 76 4.1.4 ACK 77 4.1.5 CANCEL 79 4.1.6 OPTIONS 81 4.1.7 ...
It is broadly accepted that understanding the genetic, molecular, and cellular mechanisms of neural develop- ment is essential for understanding evolution and disor- ders of neural systems. Recent ...
6.2 Understanding Cross-Layer Design 123 6.3 General Motivations for Cross-Layer Design 124 6.4 A Taxonomy of Cross-Layer Design Proposals 129 6.5 Proposals for Implementing Cross-Layer Interactions ...
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 ...
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 ...
References and More Information 53 Contents xiii 3 Writing Your First Android Application 55 Testing Your Development Environment 55 Adding the Android Samples Using the SDK Manager 56 Adding the ...
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
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++ ...
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 ...
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 ...
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 ...
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 ...
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....
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-...
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 ...
References Classes Constructors and Destructors Inheritance Summary -------------------------------------------------------------------------------- The appendix is designed to provide you ...
The HTML5 Programmer's Reference aims to provide everything a programmer needs for understanding and using the new ... The ...
Includes references to individual sections of the 3GPP specifications, to help readers understand the principles of each topic before going to the specifications for more detailed information ...