Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1392855
  • 博文数量: 335
  • 博客积分: 4690
  • 博客等级: 上校
  • 技术积分: 4337
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-08 21:38
个人简介

无聊之人--除了技术,还是技术,你懂得

文章分类

全部博文(335)

文章存档

2016年(29)

2015年(18)

2014年(7)

2013年(86)

2012年(90)

2011年(105)

分类: Python/Ruby

2011-07-01 23:45:03

5.4. Instantiating Classes

5.4 实例化类

Instantiating classes in Python is straightforward. To instantiate a class, simply call the class as if it were a function, passing the arguments that the __init__ method defines. The return value will be the newly created object.

Python 类是直接被实例化的。为了实例化一个类,简单的调用该类,就像它是一个函数一样,传递__init__方法所定义的参数。返回值将是新创建的对象。

Example 5.7. Creating a FileInfo Instance

5.7 创建一个FileInfo类实例

  1. >>> import fileinfo
  2. >>> f = fileinfo.FileInfo("/music/_singles/kairo.mp3")
  3. >>> f.__class__
  4. <class fileinfo.FileInfo at 010EC204>
  5. >>> f.__doc__
  6. 'store file metadata'
  7. >>> f
  8. {'name': '/music/_singles/kairo.mp3'}

1

You are creating an instance of the FileInfo class (defined in the fileinfo module) and assigning the newly created instance to the variable f. You are passing one parameter, /music/_singles/kairo.mp3, which will end up as the filename argument in FileInfo's __init__ method.

你创建了类FileInfo的一个实例(定在模块fileinfo中),并将新创建的对象赋值给变量f。你传递进去了一个参数, /music/_singles/kairo.mp3,该参数将赋值给类FileInfo__init_方法的filename参数

 

 

2

Every class instance has a built-in attribute, __class__, which is the object's class. (Note that the representation of this includes the physical address of the instance on my machine; your representation will be different.) Java programmers may be familiar with the Class class, which contains methods like getName and getSuperclass to get metadata information about an object. In Python, this kind of metadata is available directly on the object itself through attributes like __class__, __name__, and __bases__.

每一个类实例都包含有一个内置的属性,__class__,也就是对象类。(注意该属性的值包含了该实例在我的机器上面的物理地址,你的形式可能回看起来不同。Java编程人员对Class类非常熟悉,该类包含如getName,getSuperclass的方法一用来获取该对象的元信息。在Python中,这种元数据直接可用,直接可以通过对象本身属性如__class__,__name__,__base__来获得。

 

 

3

You can access the instance's doc string just as with a function or a module. All instances of a class share the same doc string.

就像访问函数或是模块的doc string一样,你也可以访问实例的doc string。一个类的所有实例共享相同的doc string.

 

 

4

Remember when the __init__ method assigned its filename argument to self["name"]? Well, here's the result. The arguments you pass when you create the class instance get sent right along to the __init__ method (along with the object reference, self, which Python adds for free).

记住每当__init__方法将它的filename赋值给self[“name”]时,这里就是结果。当你在创建一个对象时,你传递的参数被持续的传送给__init__方法(伴随着对象引用,self,这是Python自动添加的)。

 

 

 

Note

 

 

In Python, simply call a class as if it were a function to create a new instance of the class. There is no explicit new operator like C++ or Java.

Python中,如同调用函数一样来调用一个类来创建一个新的类实例。这儿没有C++或是java中的显式操作符:new

 

 

5.4.1. Garbage Collection

5.4.1 垃圾收集机制

If creating new instances is easy, destroying them is even easier. In general, there is no need to explicitly free instances, because they are freed automatically when the variables assigned to them go out of scope. Memory leaks are rare in Python.

如果创建对象十分容易,销毁它们甚至更容易。通常不需要显式的释放实例,因为这些实例自动被释放当被实例赋值的变量超出作用域时。Python中内存泄露十分罕见。

Example 5.8. Trying to Implement a Memory Leak

5.8 尝试实现内存泄露

    

  1. >>> def leakmem():
  2. ... f = fileinfo.FileInfo('/music/_singles/kairo.mp3')
  3. ...
  4. >>> for i in range(100):
  5. ... leakmem()

                                  2

1

Every time the leakmem function is called, you are creating an instance of FileInfo and assigning it to the variable f, which is a local variable within the function. Then the function ends without ever freeing f, so you would expect a memory leak, but you would be wrong. When the function ends, the local variable f goes out of scope. At this point, there are no longer any references to the newly created instance of FileInfo (since you never assigned it to anything other than f), so Python destroys the instance for us.

每次当函数leakmem被调用,你都创建了类FileInfo的一个实例,并将该实例赋值给变量f,f是函数内的一个局部变量。函数在结束的时候没有释放它,你可能会认为此处会产生一个内存泄露,但是你错了。当函数结束的时候,局部变量f超出了作用域。在这时,对新创建的FileInfo类实例的引用已经没有必要(因为你除了将该实例赋值给f以外没有赋值给任何其它变量),因此Python替我们销毁了该实例。

2

No matter how many times you call the leakmem function, it will never leak memory, because every time, Python will destroy the newly created FileInfo class before returning from leakmem.

不论你调用多少次函数leakmem,它从不泄露内存,这时因为在每次从函数leakmem返回值之前,Python都将销毁新创建的类FileInfo实例。

The technical term for this form of garbage collection is “reference counting”. Python keeps a list of references to every instance created. In the above example, there was only one reference to the FileInfo instance: the local variable f. When the function ends, the variable f goes out of scope, so the reference count drops to 0, and Python destroys the instance automatically.

这种形式的垃圾收集的技术术语是引用计数。Python对所有创建的对象都维护一个引用列表。在上面的例子中,类FileInfo实例仅含有一个引用:局部变量f。当函数结束时,变量f超出作用域,因此引用计数减为零,Python因此自动的销毁该实例。

In previous versions of Python, there were situations where reference counting failed, and Python couldn't clean up after you. If you created two instances that referenced each other (for instance, a doubly-linked list, where each node has a pointer to the previous and next node in the list), neither instance would ever be destroyed automatically because Python (correctly) believed that there is always a reference to each instance. Python 2.0 has an additional form of garbage collection called “mark-and-sweep” which is smart enough to notice this virtual gridlock and clean up circular references correctly.

在先前版本的Python中,存在引用计数失效的情况,Python并没有为你自动销毁实例对象。如果你创建了两个实例,彼此引用(比如,双向链表,列表的中的每一个节点都一个指针指向其前去节点和后继节点。任何一个实例都不会被自动销毁,因为Python(正确的)认为对每一个对象都含有一个引用。Python2.0拥有了一个额外的垃圾收集机制被称为”mark-and-sweep”,,该机制能智能的发现这些虚拟块锁并能争取的清除整个引用循环。

As a former philosophy major, it disturbs me to think that things disappear when no one is looking at them, but that's exactly what happens in Python. In general, you can simply forget about memory management and let Python clean up after you.

作为一个形而上学的人,,它困扰着我让我认为当没有人注视的时候,事情就消失了,但是这正是Python中发生的事情。通常,你可以简单的忘记内存管理并在Python在你后面进行清理。

 

阅读(1037) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~