DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Edited on

U - Glossary (Python)

Buy Me a Coffee

union type

  • means a set of multiple types in talking about types because it's inferred as Union by PyCo at ptime:
    • PyCo at ptime is more important program than type checkers at pptime.

unpack operator

  • is * and **:

    • *:

      • can unpack:
        • an iterable as an item, e.g. *[0, 1, 2], *{'A':0, 'B':1} and *'ABC'.
        • a tuple as a type, e.g. *tuple[int, ...] and *Tu:
          • Unpack can also unpack tuple as a type, e.g. Unpack[tuple[int, ...]] and Unpack[Tu].
      • can receive:

        • unpacked or unpacked-like items:
          • E.g. a, *b, c = [0, 1, 2, 3, 4].
          • E.g. for a, *b, c in [[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]: ....
          • E.g. def func(a, b, *args): ...; func(*[0, 1, 2, 3, 4]).
          • E.g. def func(a, b, *args): ...; func(0, 1, 2, 3, 4).
        • unpacked or unpacked-like types:

           # E.g.
           class Cls[T, *Tu]: ...
          
           v1: Cls[*tuple[int, str, int, str, int]]
           v2: Cls[int, str, int, str, int]
          
    • **:

      • can unpack a dictionary as an item:
        • E.g. **{'a':0, 'b':1}.
      • can receive:

        • unpacked or unpacked-like items:

           # E.g.
           def func(a, b, **kwargs): ...
          
           func(**{'a':0, 'b':1, 'c':2, 'd':3})
           func(a=0, b=1, c=2, d=3)
          
      • doesn't exist to unpack a dictionary as a type, and to receive unpacked or unpacked-like types:

        • Unpack can unpack the dictionary based on TypedDict as a type, e.g. Unpack[MyTypedDict].
        • ** of **P to receive a paramtypespec is a paramtypespecoper but not an unpack operator.
  • can also be called unpacking operator, packing operator, iterable unpacking operator, dictionary unpacking operator, etc but I use unpack operator to cover all the preceding terms.

unpacked item

  • is an item which is actually unpacked from an iterable:

    # E.g.  # ↓  ↓  ↓ Unpacked items
    v1 = [0, *[1, 2, 3], 4]
    v2 = {'a':0, **{'b':1, 'c':2}, 'd':3}
                  # ↑↑↑↑↑  ↑↑↑↑↑ Unpacked items
    print(v1) # [0, 1, 2, 3, 4]
    print(v2) # {'a': 0, 'b': 1, 'c': 2, 'd': 3}
    

    # E.g.  # ↓  ↓  ↓ Unpacked items
    a, b, c = [0, 1, 2]
    
    print(a, b, c) # 0 1 2
    

    # E.g.    # ↓  ↓  ↓  ↓  ↓ Unpacked items
    a, *b, c = [0, 1, 2, 3, 4]
    
    print(a, b, c) # 0 [1, 2, 3] 4
    

    # E.g.         # ↓  ↓  ↓    ↓  ↓  ↓ Unpacked items
    for a, b, c in [[0, 1, 2], [3, 4, 5]]:
        print(a, b, c) # 0 1 2
                       # 3 4 5
    

    # E.g.          # ↓  ↓  ↓  ↓  ↓    ↓  ↓  ↓  ↓  ↓ Unpacked items
    for a, *b, c in [[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]:
        print(a, b, c) # 0 [1, 2, 3] 4
                       # 5 [6, 7, 8] 9
    

    # E.g.
    def func(a, b, *args):
        print(a, b, args) # 0 1 (2, 3, 4)
    
    func(*[0, 1, 2, 3, 4])
         # ↑  ↑  ↑  ↑  ↑ Unpacked items
    

    # E.g.
    def func(a, b, **kwargs):
        print(a, b, kwargs) # 0 1 {'c': 2, 'd': 3}
    
    func(**{'a':0, 'b':1, 'c':2, 'd':3})
          # ↑↑↑↑↑  ↑↑↑↑↑  ↑↑↑↑↑  ↑↑↑↑↑ Unpacked items
    

unpacked object

  • is the general term for unpacked item and unpacked type.

unpacked type

  • is a type which is actually unpacked from a tuple or TypedDict:

    # E.g.
    from typing import Unpack
                           # ↓↓↓  ↓↓↓     Unpacked types     ↓↓↓  ↓↓↓
    def func(x: tuple[*tuple[int, ...]], *args: Unpack[tuple[int, ...]]):
        print(x, args) # (0, 1, 2) (3, 4, 5)
    
    func((0, 1, 2), *(3, 4, 5))
    

    # E.g.
    from typing import TypedDict, Unpack
    
    class TD(TypedDict):
        name: str
        age: int            # `name: str` and `age: int`
                            # ↓↓ are unpacked types
    def func(**kwargs: Unpack[TD]):
        print(kwargs) # {'name': 'John', 'age': 37}
    
    v: TD = {'name': 'John', 'age': 37}
    
    func(**v)
    

unpacked-like item

  • is an item which isn't actually unpacked from an iterable but behaves like an unpacked item to be received by *args and **kwargs:

    # E.g.
    def func(a, b, *args):
        print(a, b, args) # 0 1 (2, 3, 4)
             # ↓  ↓  ↓ Unpacked-like items
    func(0, 1, 2, 3, 4)
    func(0, 1, 2, *[3, 4])
             # ↑ An unpacked-like item
    

    # E.g.
    def func(a, b, **kwargs):
        print(a, b, kwargs) # 0 1 {'c': 2, 'd': 3, 'e': 4}
                 # ↓↓↓  ↓↓↓  ↓↓↓ Unpacked-like items
    func(a=0, b=1, c=2, d=3, e=4)
    func(a=0, b=1, c=2, **{'d':3, 'e':4})
                 # ↑↑↑ An unpacked-like item
    

unpacked-like object

  • is the general term for unpacked-like item and unpacked-like type.

unpacked-like type

  • is a type which isn't actually unpacked from a tuple but behaves like an unpacked type to be received by *Tu:

    from typing import Callable
    
    type TA[T1, T2, *Tu] = Callable[[T1, T2, *Tu], None]
    
    lam = lambda x, y, *args: print(x, y, args)
                   # ↓↓↓  ↓↓↓  ↓↓↓ Unpacked-like types
    v1: TA[int, int, int, int, int] = lam
    v2: TA[int, int, int, *tuple[int, int]] = lam
                   # ↑↑↑ An unpacked-like type
    v1(0, 1, 2, 3, 4)
    v2(0, 1, 2, 3, 4)
    # 0 1 (2, 3, 4)
    

unstrict mode

  • means not to check strictly.

untyped

  • means not to have an explicit type as a type hint.

Top comments (0)