diff --git a/README.md b/README.md index 54e72c9..892fba0 100644 --- a/README.md +++ b/README.md @@ -226,17 +226,42 @@ def count(start, step): Type ---- +* **Everything is an object.** +* **Every object has a type.** +* **Type and class are synonymous.** + +```python + = type() # Or: = .__class__ + = isinstance(, ) # Also true if 'type' is a superclass of el's type. +``` + +```python + = .__bases__ # A tuple of type's parents. + = .mro() # Returns a list of all type's superclasses. + = issubclass(, ) # Checks if 'sub_type' is a subclass of 'type'. +``` + +* **Every class is a subclass and a superclass of itself.** + ```python - = type() # / / ... +>>> type('a'), 'a'.__class__, str +(, , ) ``` +#### Some types do not have builtin names, so they must be imported: +```python +from types import FunctionType, MethodType, LambdaType, GeneratorType +``` + +### ABC-s ```python from numbers import Integral, Rational, Real, Complex, Number = isinstance(, Number) ``` ```python - = callable() +from collections.abc import Iterable, Collection, Sequence + = isinstance(, Iterable) ``` @@ -1514,6 +1539,32 @@ class MyClass(metaclass=MyMetaClass): ('abcde', 12345) ``` +#### Type diagram ('abc' is a str, str is a type, ...): +```text +┏━━━━━━━━━┯━━━━━━━━━━━━━┓ +┃ classes │ metaclasses ┃ +┠─────────┼─────────────┨ +┃ MyClass → MyMetaClass ┃ +┃ │ ↓ ┃ +┃ object ───→ type ←╮ ┃ +┃ │ ↑ ╰───╯ ┃ +┃ str ───────╯ ┃ +┗━━━━━━━━━┷━━━━━━━━━━━━━┛ +``` + +#### Inheritance diagram (str inherits from object, ...): +```text +┏━━━━━━━━━┯━━━━━━━━━━━━━┓ +┃ classes │ metaclasses ┃ +┠─────────┼─────────────┨ +┃ MyClass │ MyMetaClass ┃ +┃ ↓ │ ↓ ┃ +┃ object ←─── type ┃ +┃ ↑ │ ┃ +┃ str │ ┃ +┗━━━━━━━━━┷━━━━━━━━━━━━━┛ +``` + Operator -------- diff --git a/index.html b/index.html index 84883ec..7860b8b 100644 --- a/index.html +++ b/index.html @@ -302,12 +302,33 @@ Point(x=1, y=2 (10, 12, 14)

#Type

-
<type> = type(<el>)  # <class 'int'> / <class 'str'> / ...
+
    +
  • Everything is an object.
  • +
  • Every object has a type.
  • +
  • Type and class are synonymous.
  • +
+
<type>  = type(<el>)                      # Or: <type> = <el>.__class__
+<bool>  = isinstance(<el>, <type>)        # Also true if 'type' is a superclass of el's type.
+
+
<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.
  • +
+
>>> type('a'), 'a'.__class__, str
+(<class 'str'>, <class 'str'>, <class 'str'>)
+
+

Some types do not have builtin names, so they must be imported:

+
from types import FunctionType, MethodType, LambdaType, GeneratorType
 
+

ABC-s

from numbers import Integral, Rational, Real, Complex, Number
 <bool> = isinstance(<el>, Number)
 
-
<bool> = callable(<el>)
+
from collections.abc import Iterable, Collection, Sequence
+<bool> = isinstance(<el>, Iterable)
 

#String

<str>  = <str>.strip()                       # Strips all whitespace characters from both ends.
@@ -1244,6 +1265,28 @@ param_names  = list(<sig>.parameters.keys())
 
>>> MyClass.a, MyClass.b
 ('abcde', 12345)
 
+

Type diagram ('abc' is a str, str is a type, …):

+
┏━━━━━━━━━┯━━━━━━━━━━━━━┓
+┃ classes │ metaclasses ┃
+┠─────────┼─────────────┨
+┃ MyClass → MyMetaClass ┃
+┃         │     ↓       ┃
+┃  object ───→ type ←╮  ┃
+┃         │    ↑ ╰───╯  ┃
+┃   str ───────╯        ┃
+┗━━━━━━━━━┷━━━━━━━━━━━━━┛
+
+

Inheritance diagram (str inherits from object, …):

+
┏━━━━━━━━━┯━━━━━━━━━━━━━┓
+┃ classes │ metaclasses ┃
+┠─────────┼─────────────┨
+┃ MyClass │ MyMetaClass ┃
+┃    ↓    │     ↓       ┃
+┃  object ←─── type     ┃
+┃    ↑    │             ┃
+┃   str   │             ┃
+┗━━━━━━━━━┷━━━━━━━━━━━━━┛
+

#Operator

from operator import add, sub, mul, truediv, floordiv, mod, pow, neg, abs
 from operator import eq, ne, lt, le, gt, ge