Browse Source

Type

pull/31/head
Jure Šorn 5 years ago
parent
commit
7c98b43cce
2 changed files with 4 additions and 17 deletions
  1. 11
      README.md
  2. 10
      index.html

11
README.md

@ -232,17 +232,9 @@ Type
```python
<type> = type(<el>) # Or: <type> = <el>.__class__
<bool> = isinstance(<el>, <type>) # Also true if 'type' is a superclass of el's type.
<bool> = isinstance(<el>, <type>) # Also checks subclasses and ABCs.
```
```python
<tuple> = <type>.__bases__ # A tuple of type's parents.
<list> = <type>.mro() # Returns a list of all type's superclasses.
<bool> = issubclass(<sub_type>, <type>) # Checks if 'sub_type' is a subclass of 'type'.
```
* **Every class is a subclass and a superclass of itself.**
```python
>>> type('a'), 'a'.__class__, str
(<class 'str'>, <class 'str'>, <class 'str'>)
@ -254,6 +246,7 @@ from types import FunctionType, MethodType, LambdaType, GeneratorType
```
### ABC-s
**Abstract base classes introduce virtual subclasses, that don’t inherit from a class but are still recognized by isinstance().**
```python
from numbers import Integral, Rational, Real, Complex, Number
<bool> = isinstance(<el>, Number)

10
index.html

@ -308,15 +308,8 @@ Point(x=<span class="hljs-number">1</span>, y=<span class="hljs-number">2</span>
<li><strong>Type and class are synonymous.</strong></li>
</ul>
<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>
&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>
&lt;bool&gt; = isinstance(&lt;el&gt;, &lt;type&gt;) <span class="hljs-comment"># Also checks subclasses and ABCs.</span>
</code></pre>
<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>
&lt;list&gt; = &lt;type&gt;.mro() <span class="hljs-comment"># Returns a list of all type's superclasses.</span>
&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>
</code></pre>
<ul>
<li><strong>Every class is a subclass and a superclass of itself.</strong></li>
</ul>
<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
(&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;)
</span></code></pre>
@ -324,6 +317,7 @@ Point(x=<span class="hljs-number">1</span>, y=<span class="hljs-number">2</span>
<pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> types <span class="hljs-keyword">import</span> FunctionType, MethodType, LambdaType, GeneratorType
</code></pre>
<h3 id="abcs">ABC-s</h3>
<p><strong>Abstract base classes introduce virtual subclasses, that don’t inherit from a class but are still recognized by isinstance().</strong></p>
<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
&lt;bool&gt; = isinstance(&lt;el&gt;, Number)
</code></pre>

Loading…
Cancel
Save