Changeset 259

Show
Ignore:
Timestamp:
Tue Dec 13 14:06:50 2005
Author:
limodou
Message:

--

Files:

Legend:

Unmodified
Added
Removed
Modified
  • zh-translations/branches/diveintopython-zh-5.4/zh-cn/xml/fileinfo.xml

    r252 r259  
    197 197 <section id="fileinfo.class">  
    198 198 <?dbhtml filename="object_oriented_framework/defining_classes.html"?>  
    199   <title>Defining Classes</title>  
      199 <title>类的定义</title>  
    199 199 <abstract>  
    200 200 <title/>  
     
    311 311 <section id="fileinfo.create">  
    312 312 <?dbhtml filename="object_oriented_framework/instantiating_classes.html"?>  
    313   <title>Instantiating Classes</title>  
      313 <title>类的实例化</title>  
    313 313 <abstract>  
    314 314 <title/>  
    315   <para>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.</para>  
      315 <para>在 &python; 中对类进行实例化很直接。为了对类进行实例化,只要调用类,好象它是一个函数,传入定义在 &init; 方法中的参数。返回值将是新创建的对象。</para>  
    315 315 </abstract>  
    316 316 <example>  
    317   <title>Creating a <classname>FileInfo</classname> Instance</title>  
      317 <title>创建 <classname>FileInfo</classname> 实例</title>  
    317 317 <screen>&prompt;<userinput>import &fileinfo_name;</userinput>  
    318 318 &prompt;<userinput>f = fileinfo.FileInfo("/music/_singles/kairo.mp3")</userinput> <co id="fileinfo.create.1.1"/>  
     
    328 328 <calloutlist>  
    329 329 <callout arearefs="fileinfo.create.1.1">  
    330   <para>You are creating an instance of the <classname>FileInfo</classname> class (defined in the &fileinfo_modulename; module) and assigning the newly created instance to the variable <varname>f</varname>.  You are passing one parameter, <literal>/music/_singles/kairo.mp3</literal>, which will end up as the <varname>filename</varname> argument in <classname>FileInfo</classname>'s &init; method.</para>  
      330 <para>你正在创建 <classname>FileInfo</classname> 类(定义在 &fileinfo_modulename; 模块中)的实例,并且将新创建的实例赋值给变量 <varname>f</varname>。你传入了一个参数,<literal>/music/_singles/kairo.mp3</literal>,它将最后作为在 <classname>FileInfo</classname> 中 &init; 方法中的 <varname>filename</varname> 参数。</para>  
    330 330 </callout>  
    331 331 <callout arearefs="fileinfo.create.1.2">  
    332   <para>Every class instance has a built-in attribute, &classattr;, 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 <classname>Class</classname> class, which contains methods like <function>getName</function> and <function>getSuperclass</function> to get metadata information about an object.  In &python;, this kind of metadata is available directly on the object itself through attributes like &classattr;, <literal>__name__</literal>, and <literal>__bases__</literal>.</para>  
      332 <para>每一个类的实例有一个内置属性, &classattr;,它是对象的类。(注意这个表示包括了在我机器上的实例的物理地址,你的表示不会一样。)&java; 程序员可能对 <classname>Class</classname> 类熟悉,这个类包含了象 <function>getName</function> 和 <function>getSuperclass</function>  之类用来得到一个对象元数据信息的方法。在 &python; 中,这类元数据可以直接通过对象本身的属性,象 &classattr;, <literal>__name__</literal>, 和 <literal>__bases__</literal> 来得到。</para>  
    332 332 </callout>  
    333 333 <callout arearefs="fileinfo.create.1.3">  
    334   <para>You can access the instance's &docstring; just as with a function or a module.  All instances of a class share the same &docstring;.</para>  
      334 <para>你可以象对函数或模块一样来访问实例的 &docstring;。一个类的所有实例共享相同的 &docstring;。</para>  
    334 334 </callout>  
    335 335 <callout arearefs="fileinfo.create.1.4">  
    336   <para>Remember when the &init; method <link linkend="fileinfo.class.example">assigned its <varname>filename</varname> argument to <literal>self["name"]</literal></link>?  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).</para>  
      336 <para>还记得什么时候 &init; 方法<link linkend="fileinfo.class.example">将它的 <varname>filename</varname> 参数赋给 <literal>self["name"]</literal></link> 吗?哦,答案在这。在创建类实例时你传入的参数被正确发送到 &init; 方法中(当我们创建类实例时,我们所传递的参数被正确地发送给 __init__ 方法(随同一起传递的还有对象的引用,&self;,它是由 &python; 自动添加的)。</para>  
    336 336 </callout>  
    337 337 </calloutlist>  
    338 338 </example>  
    339 339 <note id="compare.new.java" role="compare" vendor="java">  
    340   <title>&python; &vs; &java;: Instantiating Classes</title>  
    341   <para>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 &cpp; or &java;.</para>  
      340 <title>&python; &vs; &java;: 类的实例化</title>  
      341 <para>在 &python; 中,创建类的实例只要调用一个类,仿佛它是一个函数就行了。不象 &cpp; 或 &java; 有一个显式的 &new; 操作符。</para>  
    342 342 </note>  
    343 343 <section>  
    344   <title>Garbage Collection</title>  
    345   <para>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;.</para>  
      344 <title>垃圾回收</title>  
      345 <para>如果如创建一个新的实例是容易的,那么销毁它们甚至更容易。通常,不需要显式地释放实例,因为当指派给它们的变量超出作用域时,它们会被自动地释放。内存泄漏在 &python; 中很少见。</para>  
    346 346 <example id="fileinfo.scope">  
    347   <title>Trying to Implement a Memory Leak</title>  
      347 <title>尝试实现内存泄漏</title>  
    347 347 <screen>&prompt;<userinput>def leakmem():</userinput>  
    348 348 &continuationprompt;<userinput>f = fileinfo.FileInfo('/music/_singles/kairo.mp3')</userinput> <co id="fileinfo.create.2.1"/>  
    357 357 <calloutlist>  
    358 358 <callout arearefs="fileinfo.create.2.1">  
    359   <para>Every time the <function>leakmem</function> function is called, you are creating an instance of <classname>FileInfo</classname> and assigning it to the variable <varname>f</varname>, which is a local variable within the function.  Then the function ends without ever freeing <varname>f</varname>, so you would expect a memory leak, but you would be wrong.  When the function ends, the local variable <varname>f</varname> goes out of scope.  At this point, there are no longer any references to the newly created instance of <classname>FileInfo</classname> (since you never assigned it to anything other than <varname>f</varname>), so &python; destroys the instance for us.</para>  
      359 <para>每次 <function>leakmem</function> 函数被调用,你创建了 &fileinfo_classname; 的一个实例,将其赋给变量 <varname>f</varname>,这个变量是函数内的一个局部变量。然后函数结束没有释放 <varname>f</varname>,所以你可能认为有内存泄漏,但是你错了。当函数结束时,局部变量 <varname>f</varname> 超出了作用域。在这个地方,不再有任何对 &fileinfo_classname; 新创建实例的引用(因为除了 <varname>f</varname> 我们从未将其赋值给其它变量),所以 &python; 替我们销毁掉实例。</para>  
    359 359 </callout>  
    360 360 <callout arearefs="fileinfo.create.2.3">  
    361   <para>No matter how many times you call the <function>leakmem</function> function, it will never leak memory, because every time, &python; will destroy the newly created &fileinfo_classname; class before returning from <function>leakmem</function>.</para>  
      361 <para>不管我们调用 <function>leakmem</function> 函数多少次,决不会泄漏内存,因为每一次,&python; 将在从 <function>leakmem</function> 返回前销毁掉新创建的 &fileinfo_classname; 类实例。</para>  
    361 361 </callout>  
    362 362 </calloutlist>  
    363 363 </example>  
    364   <para>The technical term for this form of garbage collection is <quote>reference counting</quote>.  &python; keeps a list of references to every instance created.  In the above example, there was only one reference to the &fileinfo_classname; instance: the local variable <varname>f</varname>.  When the function ends, the variable <varname>f</varname> goes out of scope, so the reference count drops to &zero;, and &python; destroys the instance automatically.</para>  
    365   <para>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 <quote>mark-and-sweep</quote> which is smart enough to notice this virtual gridlock and clean up circular references correctly.</para>  
    366   <para>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.</para>  
      364 <para>对于这种垃圾收集的方式,技术上的术语叫做<quote>引用计数</quote>。&python; 维护着对每个实例的引用列表。在上面的例子中,对于 &fileinfo_classname; 实例只有一个引用:局部变量 <varname>f</varname>。当函数结束时,变量 <varname>f</varname> 超出作用域,所以引用计数降为 &zero;,则 &python; 自动销毁掉实例。</para>  
      365 <para>在 &python; 的以前版本中,存在引用计数失败的情况,这样 &python; 不能在后面进行清除。如果你创建两个实例,它们相互引用(例如,双重链表,每一个结点有都一个指向列表中前一个和后一个结点的指针),任一个实例都不会被自动销毁,因为 &python; (正确)认为对于每个实例都存在一个引用。 &python; 2.0有一种额外的垃圾回收方式,叫做<quote>标记后清除</quote>,它足够聪明,可以正确地清除循环引用。</para>  
      366 <para>作为以前主修哲学专业的人来说,让我感到困惑的是,思索当没有人对事物进行观察时,它们就消失了,但是这确实是在 &python; 中所发生的。通常,你可以完全忘记内存管理,让 &python; 在后面进行清理。</para>  
    367 367 <itemizedlist role="furtherreading">  
    368   <title>Further Reading on Garbage Collection</title>  
    369   <listitem><para>&pythonlibraryreference; summarizes <ulink url="&url_pythonlibraryreference;specialattrs.html">built-in attributes like &classattr;</ulink>.</para></listitem>  
    370   <listitem><para>&pythonlibraryreference; documents the <ulink url="&url_pythonlibraryreference;module-gc.html"><filename class="headerfile">gc</filename> module</ulink>, which gives you low-level control over &python;'s garbage collection.</para></listitem>  
      368 <title>进一步阅读关于垃圾回收</title>  
      369 <listitem><para>&pythonlibraryreference; 总结了 <ulink url="&url_pythonlibraryreference;specialattrs.html">象 &classattr; 之类的内置属性</ulink>。</para></listitem>  
      370 <listitem><para>&pythonlibraryreference; 提供了 <ulink url="&url_pythonlibraryreference;module-gc.html"><filename class="headerfile">gc</filename> 模块的文档</ulink>,此模块它给予你对 &python; 的垃圾回收的底层控制权。</para></listitem>  
    371 371 </itemizedlist>  
    372 372 </section>  
  • zh-translations/branches/diveintopython-zh-5.4/zh-cn/xml/bookinfo.xml

    r238 r259  
    763 763 <para>允许在遵守 <acronym>GNU</acronym> 自由文档许可证(GNU Free Documentation License,版本1.1或由自由软件基金会公布的最新版本)条款的规定下,拷贝,发布,并且/或者修改这个文档。本文档没有不可变部分,没有封面文字,没有封底文字。在<xref linkend="gfdl"/>中包含了此许可证的一份拷贝。</para>  
    764 764 <para>在这本书中的例程是自由软件。你可以在遵守 &python; 许可证(&python; 软件基金会发布)条款的规定下,重新发布,并且/或者修改它们。在<xref linkend="license"/>中包含了此许可证的一份拷贝。</para>  
    765   <para>本译本由limodou(limodou AT gmail.com), Osmond(sinosmond AT gmail.com), YuLin(yulin724 AT gmail.com)等翻译。由 Zoom.Quiet(zoom.quiet AT gmail.com) 负责项目管理。感谢<ulink url="http://www.woodpecker.org.cn">啄木鸟社区</ulink>提供svn和项目空间。本译文遵守GFDL的规定。你可以拷贝,发布,修改此文档,但请保留此版权信息。</para>  
      765 <para>本译本由limodou(limodou AT gmail.com), Osmond(sinosmond AT gmail.com), YuLin(yulin724 AT gmail.com), Zoom.Quiet(zoom.quiet AT gmail.com)等翻译。由 Zoom.Quiet(zoom.quiet AT gmail.com) 负责项目管理。感谢<ulink url="http://www.woodpecker.org.cn">啄木鸟社区</ulink>提供svn和项目空间。本译文遵守GFDL的规定。你可以拷贝,发布,修改此文档,但请保留此版权信息。</para>  
    765 765 </legalnotice>  
    766 766 <!--