@@ -308,22 +308,16 @@ <h2 id="type"><a href="#type" name="type">#</a>Type</h2>
308
308
< li > < strong > Type and class are synonymous.</ strong > </ li >
309
309
</ ul >
310
310
< pre > < code class ="python language-python hljs "> <type> = type(<el>) < span class ="hljs-comment "> # Or: <type> = <el>.__class__</ span >
311
- <bool> = isinstance(<el>, <type>) < span class ="hljs-comment "> # Also true if 'type' is a superclass of el's type .</ span >
311
+ <bool> = isinstance(<el>, <type>) < span class ="hljs-comment "> # Also checks subclasses and ABCs .</ span >
312
312
</ code > </ pre >
313
- < pre > < code class ="python language-python hljs "> <tuple> = <type>.__bases__ < span class ="hljs-comment "> # A tuple of type's parents.</ span >
314
- <list> = <type>.mro() < span class ="hljs-comment "> # Returns a list of all type's superclasses.</ span >
315
- <bool> = issubclass(<sub_type>, <type>) < 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 >
320
313
< pre > < code class ="python language-python hljs "> < span class ="hljs-meta "> >>> </ span > type(< span class ="hljs-string "> 'a'</ span > ), < span class ="hljs-string "> 'a'</ span > .__class__, str
321
314
(<< span class ="hljs-class "> < span class ="hljs-title "> class</ span > '< span class ="hljs-title "> str</ span > '>, << span class ="hljs-title "> class</ span > '< span class ="hljs-title "> str</ span > '>, << span class ="hljs-title "> class</ span > '< span class ="hljs-title "> str</ span > '>)
322
315
</ span > </ code > </ pre >
323
316
< h4 id ="sometypesdonothavebuiltinnamessotheymustbeimported "> Some types do not have builtin names, so they must be imported:</ h4 >
324
317
< pre > < code class ="python language-python hljs "> < span class ="hljs-keyword "> from</ span > types < span class ="hljs-keyword "> import</ span > FunctionType, MethodType, LambdaType, GeneratorType
325
318
</ code > </ pre >
326
319
< 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 >
327
321
< 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
328
322
<bool> = isinstance(<el>, Number)
329
323
</ code > </ pre >
0 commit comments