Skip to content

Commit 7c98b43

Browse files
committed
Type
1 parent b053d86 commit 7c98b43

File tree

2 files changed

+4
-17
lines changed

2 files changed

+4
-17
lines changed

README.md

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -232,17 +232,9 @@ Type
232232

233233
```python
234234
<type> = type(<el>) # Or: <type> = <el>.__class__
235-
<bool> = isinstance(<el>, <type>) # Also true if 'type' is a superclass of el's type.
235+
<bool> = isinstance(<el>, <type>) # Also checks subclasses and ABCs.
236236
```
237237

238-
```python
239-
<tuple> = <type>.__bases__ # A tuple of type's parents.
240-
<list> = <type>.mro() # Returns a list of all type's superclasses.
241-
<bool> = issubclass(<sub_type>, <type>) # Checks if 'sub_type' is a subclass of 'type'.
242-
```
243-
244-
* **Every class is a subclass and a superclass of itself.**
245-
246238
```python
247239
>>> type('a'), 'a'.__class__, str
248240
(<class 'str'>, <class 'str'>, <class 'str'>)
@@ -254,6 +246,7 @@ from types import FunctionType, MethodType, LambdaType, GeneratorType
254246
```
255247

256248
### ABC-s
249+
**Abstract base classes introduce virtual subclasses, that don’t inherit from a class but are still recognized by isinstance().**
257250
```python
258251
from numbers import Integral, Rational, Real, Complex, Number
259252
<bool> = isinstance(<el>, Number)

index.html

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -308,22 +308,16 @@ <h2 id="type"><a href="#type" name="type">#</a>Type</h2>
308308
<li><strong>Type and class are synonymous.</strong></li>
309309
</ul>
310310
<pre><code class="python language-python hljs">&lt;type&gt; = type(&lt;el&gt;) <span class="hljs-comment"># Or: &lt;type&gt; = &lt;el&gt;.__class__</span>
311-
&lt;bool&gt; = isinstance(&lt;el&gt;, &lt;type&gt;) <span class="hljs-comment"># Also true if 'type' is a superclass of el's type.</span>
311+
&lt;bool&gt; = isinstance(&lt;el&gt;, &lt;type&gt;) <span class="hljs-comment"># Also checks subclasses and ABCs.</span>
312312
</code></pre>
313-
<pre><code class="python language-python hljs">&lt;tuple&gt; = &lt;type&gt;.__bases__ <span class="hljs-comment"># A tuple of type's parents.</span>
314-
&lt;list&gt; = &lt;type&gt;.mro() <span class="hljs-comment"># Returns a list of all type's superclasses.</span>
315-
&lt;bool&gt; = issubclass(&lt;sub_type&gt;, &lt;type&gt;) <span class="hljs-comment"># Checks if 'sub_type' is a subclass of 'type'.</span>
316-
</code></pre>
317-
<ul>
318-
<li><strong>Every class is a subclass and a superclass of itself.</strong></li>
319-
</ul>
320313
<pre><code class="python language-python hljs"><span class="hljs-meta">&gt;&gt;&gt; </span>type(<span class="hljs-string">'a'</span>), <span class="hljs-string">'a'</span>.__class__, str
321314
(&lt;<span class="hljs-class"><span class="hljs-title">class</span> '<span class="hljs-title">str</span>'&gt;, &lt;<span class="hljs-title">class</span> '<span class="hljs-title">str</span>'&gt;, &lt;<span class="hljs-title">class</span> '<span class="hljs-title">str</span>'&gt;)
322315
</span></code></pre>
323316
<h4 id="sometypesdonothavebuiltinnamessotheymustbeimported">Some types do not have builtin names, so they must be imported:</h4>
324317
<pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> types <span class="hljs-keyword">import</span> FunctionType, MethodType, LambdaType, GeneratorType
325318
</code></pre>
326319
<h3 id="abcs">ABC-s</h3>
320+
<p><strong>Abstract base classes introduce virtual subclasses, that don’t inherit from a class but are still recognized by isinstance().</strong></p>
327321
<pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> numbers <span class="hljs-keyword">import</span> Integral, Rational, Real, Complex, Number
328322
&lt;bool&gt; = isinstance(&lt;el&gt;, Number)
329323
</code></pre>

0 commit comments

Comments
 (0)