mirror of
https://github.com/kennethreitz/dive-into-python3.git
synced 2026-06-05 23:10:17 +00:00
add docs.python.org links for __init__ and friends; remove confusing intro sentence about the "with" keyword
This commit is contained in:
@@ -34,14 +34,14 @@ h3:before{counter-increment:h3;content:'B.' counter(h2) '.' counter(h3) '. '}
|
||||
<tr><th>①
|
||||
<td>to initialize an instance
|
||||
<td><code class=pp>x = MyClass()</code>
|
||||
<td><code class=pp>x.<dfn>__init__</dfn>()</code>
|
||||
<td><a href=http://docs.python.org/3.1/reference/datamodel.html#object.__init__><code class=pp>x.<dfn>__init__</dfn>()</a></code>
|
||||
<tr><th>②
|
||||
<td>the “official” representation as a string
|
||||
<td><code class=pp><dfn>repr</dfn>(x)</code>
|
||||
<td><code class=pp>x.<dfn>__repr__</dfn>()</code>
|
||||
<td><a href=http://docs.python.org/3.1/reference/datamodel.html#object.__repr__><code class=pp>x.<dfn>__repr__</dfn>()</a></code>
|
||||
<tr><th>③
|
||||
<td>the “informal” value as a string
|
||||
<td><code class=pp><dfn>str</dfn>(x)</code>
|
||||
<td><a href=http://docs.python.org/3.1/reference/datamodel.html#object.__str__><code class=pp><dfn>str</dfn>(x)</a></code>
|
||||
<td><code class=pp>x.<dfn>__str__</dfn>()</code>
|
||||
<tr><th>④
|
||||
<td>the “informal” value as a byte array
|
||||
@@ -49,7 +49,7 @@ h3:before{counter-increment:h3;content:'B.' counter(h2) '.' counter(h3) '. '}
|
||||
<td><code class=pp>x.<dfn>__bytes__</dfn>()</code>
|
||||
<tr><th>⑤
|
||||
<td>the value as a formatted string
|
||||
<td><code class=pp>format(x, <var>format_spec</var>)</code>
|
||||
<td><a href=http://docs.python.org/3.1/reference/datamodel.html#object.__format__><code class=pp>format(x, <var>format_spec</var>)</a></code>
|
||||
<td><code class=pp>x.<dfn>__format__</dfn>(<var>format_spec</var>)</code>
|
||||
</table>
|
||||
<ol>
|
||||
@@ -715,7 +715,7 @@ class FieldStorage:
|
||||
|
||||
<h2 id=context-managers>Classes That Can Be Used in a <code>with</code> Block</h2>
|
||||
|
||||
<p>Python 3 supports the <code>with</code> statement, which allows you to access an object’s properties and methods without explicitly referencing the object every time. A <code>with</code> block defines a <a href=http://www.python.org/doc/3.1/library/stdtypes.html#typecontextmanager>runtime context</a>; you “enter” the context when you execute the <code>with</code> statement, and you “exit” the context after you execute the last statement in the block.
|
||||
<p>A <code>with</code> block defines a <a href=http://www.python.org/doc/3.1/library/stdtypes.html#typecontextmanager>runtime context</a>; you “enter” the context when you execute the <code>with</code> statement, and you “exit” the context after you execute the last statement in the block.
|
||||
|
||||
<p>Any class can be used in a <code>with</code> block; no special methods are required. The Python interpreter will automatically set up the runtime context and dispatch all the property and method lookups to your class. However, if you want your class to do something special upon entering or exiting a runtime context, you can define the following special methods.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user