@@ -302,12 +302,33 @@ <h2 id="generator"><a href="#generator" name="generator">#</a>Generator</h2>
302
302
(< span class ="hljs-number "> 10</ span > , < span class ="hljs-number "> 12</ span > , < span class ="hljs-number "> 14</ span > )
303
303
</ code > </ pre >
304
304
< h2 id ="type "> < a href ="#type " name ="type "> #</ a > Type</ h2 >
305
- < pre > < code class ="python language-python hljs "> <type> = type(<el>) < span class ="hljs-comment "> # <class 'int'> / <class 'str'> / ...</ span >
305
+ < ul >
306
+ < li > < strong > Everything is an object.</ strong > </ li >
307
+ < li > < strong > Every object has a type.</ strong > </ li >
308
+ < li > < strong > Type and class are synonymous.</ strong > </ li >
309
+ </ ul >
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 >
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
+ < 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
+ (<< span class ="hljs-class "> < span class ="hljs-keyword "> 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
+ </ span > </ code > </ pre >
323
+ < h4 id ="sometypesdonothavebuiltinnamessotheymustbeimported "> Some types do not have builtin names, so they must be imported:</ h4 >
324
+ < pre > < code class ="python language-python hljs "> < span class ="hljs-keyword "> from</ span > types < span class ="hljs-keyword "> import</ span > FunctionType, MethodType, LambdaType, GeneratorType
306
325
</ code > </ pre >
326
+ < h3 id ="abcs "> ABC-s</ h3 >
307
327
< 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
308
328
<bool> = isinstance(<el>, Number)
309
329
</ code > </ pre >
310
- < pre > < code class ="python language-python hljs "> <bool> = callable(<el>)
330
+ < pre > < code class ="python language-python hljs "> < span class ="hljs-keyword "> from</ span > collections.abc < span class ="hljs-keyword "> import</ span > Iterable, Collection, Sequence
331
+ <bool> = isinstance(<el>, Iterable)
311
332
</ code > </ pre >
312
333
< h2 id ="string "> < a href ="#string " name ="string "> #</ a > String</ h2 >
313
334
< pre > < code class ="python language-python hljs "> <str> = <str>.strip() < span class ="hljs-comment "> # Strips all whitespace characters from both ends.</ span >
@@ -1244,6 +1265,28 @@ <h3 id="metaclassattribute">Metaclass Attribute</h3>
1244
1265
< pre > < code class ="python language-python hljs "> < span class ="hljs-meta "> >>> </ span > MyClass.a, MyClass.b
1245
1266
(< span class ="hljs-string "> 'abcde'</ span > , < span class ="hljs-number "> 12345</ span > )
1246
1267
</ code > </ pre >
1268
+ < h4 id ="typediagramabcisastrstrisatype "> Type diagram ('abc' is a str, str is a type, …):</ h4 >
1269
+ < pre > < code class ="text language-text "> ┏━━━━━━━━━┯━━━━━━━━━━━━━┓
1270
+ ┃ classes │ metaclasses ┃
1271
+ ┠─────────┼─────────────┨
1272
+ ┃ MyClass → MyMetaClass ┃
1273
+ ┃ │ ↓ ┃
1274
+ ┃ object ───→ type ←╮ ┃
1275
+ ┃ │ ↑ ╰───╯ ┃
1276
+ ┃ str ───────╯ ┃
1277
+ ┗━━━━━━━━━┷━━━━━━━━━━━━━┛
1278
+ </ code > </ pre >
1279
+ < h4 id ="inheritancediagramstrinheritsfromobject "> Inheritance diagram (str inherits from object, …):</ h4 >
1280
+ < pre > < code class ="text language-text "> ┏━━━━━━━━━┯━━━━━━━━━━━━━┓
1281
+ ┃ classes │ metaclasses ┃
1282
+ ┠─────────┼─────────────┨
1283
+ ┃ MyClass │ MyMetaClass ┃
1284
+ ┃ ↓ │ ↓ ┃
1285
+ ┃ object ←─── type ┃
1286
+ ┃ ↑ │ ┃
1287
+ ┃ str │ ┃
1288
+ ┗━━━━━━━━━┷━━━━━━━━━━━━━┛
1289
+ </ code > </ pre >
1247
1290
< h2 id ="operator "> < a href ="#operator " name ="operator "> #</ a > Operator</ h2 >
1248
1291
< pre > < code class ="python language-python hljs "> < span class ="hljs-keyword "> from</ span > operator < span class ="hljs-keyword "> import</ span > add, sub, mul, truediv, floordiv, mod, pow, neg, abs
1249
1292
< span class ="hljs-keyword "> from</ span > operator < span class ="hljs-keyword "> import</ span > eq, ne, lt, le, gt, ge
0 commit comments