Skip to content

๐Ÿ ๐Ÿ†Ž ๐ŸŽถ

๐Ÿ โœ”๏ธ ๐Ÿ•โ€๐Ÿฆบ ๐Ÿ“ฆ "๐Ÿ†Ž ๐Ÿ”‘".

๐Ÿ‘ซ "๐Ÿ†Ž ๐Ÿ”‘" ๐ŸŽ โ• ๐Ÿ‘ˆ โœ” ๐Ÿ“ฃ ๐Ÿ†Ž ๐Ÿ”ข.

๐Ÿ“ฃ ๐Ÿ†Ž ๐Ÿ‘† ๐Ÿ”ข, ๐Ÿ‘จโ€๐ŸŽจ & ๐Ÿงฐ ๐Ÿ’ช ๐Ÿค ๐Ÿ‘† ๐Ÿ‘ ๐Ÿ•โ€๐Ÿฆบ.

๐Ÿ‘‰ โฉ ๐Ÿ”ฐ / โ†—๏ธ ๐Ÿ”ƒ ๐Ÿ ๐Ÿ†Ž ๐Ÿ”‘. โšซ๏ธ ๐Ÿ“” ๐Ÿ•ด ๐Ÿ’ฏ ๐Ÿ’ช โš™๏ธ ๐Ÿ‘ซ โฎ๏ธ FastAPI... โ” ๐Ÿค™ ๐Ÿ“ถ ๐Ÿฅ.

FastAPI ๐ŸŒ โš“๏ธ ๐Ÿ”› ๐Ÿ‘ซ ๐Ÿ†Ž ๐Ÿ”‘, ๐Ÿ‘ซ ๐Ÿค โšซ๏ธ ๐Ÿ“š ๐Ÿ“ˆ & ๐Ÿ’ฐ.

โœ‹๏ธ ๐Ÿšฅ ๐Ÿ‘† ๐Ÿ™… โš™๏ธ FastAPI, ๐Ÿ‘† ๐Ÿ”œ ๐Ÿ’ฐ โšช๏ธโžก๏ธ ๐Ÿซ ๐Ÿ– ๐Ÿ”ƒ ๐Ÿ‘ซ.

Note

๐Ÿšฅ ๐Ÿ‘† ๐Ÿ ๐Ÿ•ด, & ๐Ÿ‘† โช ๐Ÿ’ญ ๐ŸŒ ๐Ÿ”ƒ ๐Ÿ†Ž ๐Ÿ”‘, ๐Ÿšถ โญ ๐Ÿ“ƒ.

๐ŸŽฏ

โžก๏ธ โ–ถ๏ธ โฎ๏ธ ๐Ÿ™… ๐Ÿ–ผ:

def get_full_name(first_name, last_name):
    full_name = first_name.title() + " " + last_name.title()
    return full_name


print(get_full_name("john", "doe"))

๐Ÿค™ ๐Ÿ‘‰ ๐Ÿ“‹ ๐Ÿ”ข:

John Doe

๐Ÿ”ข ๐Ÿ”จ ๐Ÿ“„:

  • โœŠ first_name & last_name.
  • ๐Ÿ—œ ๐Ÿฅ‡ ๐Ÿ”ค ๐Ÿ”  1๏ธโƒฃ โ†– ๐Ÿ’ผ โฎ๏ธ title().
  • ๐Ÿ”ข ๐Ÿ‘ซ โฎ๏ธ ๐Ÿš€ ๐Ÿ–•.
def get_full_name(first_name, last_name):
    full_name = first_name.title() + " " + last_name.title()
    return full_name


print(get_full_name("john", "doe"))

โœ โšซ๏ธ

โšซ๏ธ ๐Ÿ“ถ ๐Ÿ™… ๐Ÿ“‹.

โœ‹๏ธ ๐Ÿ”œ ๐ŸŒˆ ๐Ÿ‘ˆ ๐Ÿ‘† โœ โšซ๏ธ โšช๏ธโžก๏ธ ๐Ÿ–Œ.

โ˜ ๐Ÿ‘† ๐Ÿ”œ โœ”๏ธ โ–ถ๏ธ ๐Ÿ”‘ ๐Ÿ”ข, ๐Ÿ‘† โœ”๏ธ ๐Ÿ”ข ๐Ÿ”œ...

โœ‹๏ธ โคด๏ธ ๐Ÿ‘† โœ”๏ธ ๐Ÿค™ "๐Ÿ‘ˆ ๐Ÿ‘ฉโ€๐Ÿ”ฌ ๐Ÿ‘ˆ ๐Ÿ—œ ๐Ÿฅ‡ ๐Ÿ”ค โ†– ๐Ÿ’ผ".

โšซ๏ธ upperโ“ โšซ๏ธ uppercaseโ“ first_uppercaseโ“ capitalizeโ“

โคด๏ธ, ๐Ÿ‘† ๐Ÿ”„ โฎ๏ธ ๐Ÿ— ๐Ÿ‘ฉโ€๐Ÿ’ป ๐Ÿ‘จโ€๐Ÿ‘งโ€๐Ÿ‘ฆ, ๐Ÿ‘จโ€๐ŸŽจ โœ.

๐Ÿ‘† ๐Ÿ†Ž ๐Ÿฅ‡ ๐Ÿ”ข ๐Ÿ”ข, first_name, โคด๏ธ โฃ (.) & โคด๏ธ ๐ŸŽฏ Ctrl+Space โฒ ๐Ÿ› ๏ธ.

โœ‹๏ธ, ๐Ÿ˜ž, ๐Ÿ‘† ๐Ÿคš ๐Ÿ•ณ โš :

๐Ÿšฎ ๐Ÿ†Ž

โžก๏ธ ๐Ÿ”€ ๐Ÿ‘ โธ โšช๏ธโžก๏ธ โฎ๏ธ โฌ.

๐Ÿ‘ฅ ๐Ÿ”œ ๐Ÿ”€ โšซ๏ธโ” ๐Ÿ‘‰ ๐Ÿงฌ, ๐Ÿ”ข ๐Ÿ”ข, โšช๏ธโžก๏ธ:

    first_name, last_name

:

    first_name: str, last_name: str

๐Ÿ‘ˆ โšซ๏ธ.

๐Ÿ‘ˆ "๐Ÿ†Ž ๐Ÿ”‘":

def get_full_name(first_name: str, last_name: str):
    full_name = first_name.title() + " " + last_name.title()
    return full_name


print(get_full_name("john", "doe"))

๐Ÿ‘ˆ ๐Ÿšซ ๐ŸŽ ๐Ÿ“ฃ ๐Ÿ”ข ๐Ÿ’ฒ ๐Ÿ’– ๐Ÿ”œ โฎ๏ธ:

    first_name="john", last_name="doe"

โšซ๏ธ ๐ŸŽ ๐Ÿ‘œ.

๐Ÿ‘ฅ โš™๏ธ โค (:), ๐Ÿšซ ๐ŸŒ“ (=).

& โŽ ๐Ÿ†Ž ๐Ÿ”‘ ๐Ÿ›Ž ๐Ÿšซ ๐Ÿ”€ โšซ๏ธโ” ๐Ÿ”จ โšช๏ธโžก๏ธ โšซ๏ธโ” ๐Ÿ”œ ๐Ÿ”จ ๐Ÿต ๐Ÿ‘ซ.

โœ‹๏ธ ๐Ÿ”œ, ๐ŸŒˆ ๐Ÿ‘† ๐Ÿ”„ ๐Ÿ–• ๐Ÿ— ๐Ÿ‘ˆ ๐Ÿ”ข, โœ‹๏ธ โฎ๏ธ ๐Ÿ†Ž ๐Ÿ”‘.

๐ŸŽ โ˜, ๐Ÿ‘† ๐Ÿ”„ โฒ ๐Ÿ“‹ โฎ๏ธ Ctrl+Space & ๐Ÿ‘† ๐Ÿ‘€:

โฎ๏ธ ๐Ÿ‘ˆ, ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ“œ, ๐Ÿ‘€ ๐ŸŽ›, โญ ๐Ÿ‘† ๐Ÿ”Ž 1๏ธโƒฃ ๐Ÿ‘ˆ "๐Ÿ’ ๐Ÿ””":

๐ŸŒ… ๐ŸŽฏ

โœ… ๐Ÿ‘‰ ๐Ÿ”ข, โšซ๏ธ โช โœ”๏ธ ๐Ÿ†Ž ๐Ÿ”‘:

def get_name_with_age(name: str, age: int):
    name_with_age = name + " is this old: " + age
    return name_with_age

โ†ฉ๏ธ ๐Ÿ‘จโ€๐ŸŽจ ๐Ÿ’ญ ๐Ÿ†Ž ๐Ÿ”ข, ๐Ÿ‘† ๐Ÿšซ ๐Ÿ•ด ๐Ÿคš ๐Ÿ› ๏ธ, ๐Ÿ‘† ๐Ÿคš โŒ โœ…:

๐Ÿ”œ ๐Ÿ‘† ๐Ÿ’ญ ๐Ÿ‘ˆ ๐Ÿ‘† โœ”๏ธ ๐Ÿ”ง โšซ๏ธ, ๐Ÿ—œ age ๐ŸŽป โฎ๏ธ str(age):

def get_name_with_age(name: str, age: int):
    name_with_age = name + " is this old: " + str(age)
    return name_with_age

๐Ÿ“ฃ ๐Ÿ†Ž

๐Ÿ‘† ๐Ÿ‘€ ๐Ÿ‘‘ ๐Ÿฅ‰ ๐Ÿ“ฃ ๐Ÿ†Ž ๐Ÿ”‘. ๐Ÿ”ข ๐Ÿ”ข.

๐Ÿ‘‰ ๐Ÿ‘‘ ๐Ÿฅ‰ ๐Ÿ‘† ๐Ÿ”œ โš™๏ธ ๐Ÿ‘ซ โฎ๏ธ FastAPI.

๐Ÿ™… ๐Ÿ†Ž

๐Ÿ‘† ๐Ÿ’ช ๐Ÿ“ฃ ๐ŸŒ ๐Ÿฉ ๐Ÿ ๐Ÿ†Ž, ๐Ÿšซ ๐Ÿ•ด str.

๐Ÿ‘† ๐Ÿ’ช โš™๏ธ, ๐Ÿ–ผ:

  • int
  • float
  • bool
  • bytes
def get_items(item_a: str, item_b: int, item_c: float, item_d: bool, item_e: bytes):
    return item_a, item_b, item_c, item_d, item_d, item_e

๐Ÿ’Š ๐Ÿ†Ž โฎ๏ธ ๐Ÿ†Ž ๐Ÿ”ข

๐Ÿ“ค ๐Ÿ“Š ๐Ÿ“Š ๐Ÿ‘ˆ ๐Ÿ’ช ๐Ÿ”Œ ๐ŸŽ ๐Ÿ’ฒ, ๐Ÿ’– dict, list, set & tuple. & ๐Ÿ”— ๐Ÿ’ฒ ๐Ÿ’ช โœ”๏ธ ๐Ÿ‘ซ ๐Ÿ‘ ๐Ÿ†Ž ๐Ÿ’โ€โ™‚๏ธ.

๐Ÿ‘‰ ๐Ÿ†Ž ๐Ÿ‘ˆ โœ”๏ธ ๐Ÿ”— ๐Ÿ†Ž ๐Ÿค™ "๐Ÿ’Š" ๐Ÿ†Ž. & โšซ๏ธ ๐Ÿ’ช ๐Ÿ“ฃ ๐Ÿ‘ซ, โฎ๏ธ ๐Ÿ‘ซ ๐Ÿ”— ๐Ÿ†Ž.

๐Ÿ“ฃ ๐Ÿ‘ˆ ๐Ÿ†Ž & ๐Ÿ”— ๐Ÿ†Ž, ๐Ÿ‘† ๐Ÿ’ช โš™๏ธ ๐Ÿฉ ๐Ÿ ๐Ÿ•น typing. โšซ๏ธ ๐Ÿ”€ ๐ŸŽฏ ๐Ÿ•โ€๐Ÿฆบ ๐Ÿ‘ซ ๐Ÿ†Ž ๐Ÿ”‘.

๐Ÿ†• โฌ ๐Ÿ

โ• โš™๏ธ typing ๐Ÿ”— โฎ๏ธ ๐ŸŒ โฌ, โšช๏ธโžก๏ธ ๐Ÿ 3๏ธโƒฃ.6๏ธโƒฃ โช ๐Ÿ•, โœ… ๐Ÿ 3๏ธโƒฃ.9๏ธโƒฃ, ๐Ÿ 3๏ธโƒฃ.1๏ธโƒฃ0๏ธโƒฃ, โ™’๏ธ.

๐Ÿ ๐Ÿง, ๐Ÿ†• โฌ ๐Ÿ‘Ÿ โฎ๏ธ ๐Ÿ“‰ ๐Ÿ•โ€๐Ÿฆบ ๐Ÿ‘‰ ๐Ÿ†Ž โœ & ๐Ÿ“š ๐Ÿ’ผ ๐Ÿ‘† ๐Ÿ† ๐Ÿšซ ๐Ÿ’ช ๐Ÿ—„ & โš™๏ธ typing ๐Ÿ•น ๐Ÿ“ฃ ๐Ÿ†Ž โœ.

๐Ÿšฅ ๐Ÿ‘† ๐Ÿ’ช โš’ ๐ŸŒ– โฎ๏ธ โฌ ๐Ÿ ๐Ÿ‘† ๐Ÿ—, ๐Ÿ‘† ๐Ÿ”œ ๐Ÿ’ช โœŠ ๐Ÿ“ˆ ๐Ÿ‘ˆ โž• ๐Ÿฆ. ๐Ÿ‘€ ๐Ÿ–ผ ๐Ÿ”›.

๐Ÿ“‡

๐Ÿ–ผ, โžก๏ธ ๐Ÿ”ฌ ๐Ÿ”ข list str.

โšช๏ธโžก๏ธ typing, ๐Ÿ—„ List (โฎ๏ธ ๐Ÿ”  L):

from typing import List


def process_items(items: List[str]):
    for item in items:
        print(item)

๐Ÿ“ฃ ๐Ÿ”ข, โฎ๏ธ ๐ŸŽ โค (:) โ•.

๐Ÿ†Ž, ๐Ÿšฎ List ๐Ÿ‘ˆ ๐Ÿ‘† ๐Ÿ—„ โšช๏ธโžก๏ธ typing.

๐Ÿ“‡ ๐Ÿ†Ž ๐Ÿ‘ˆ ๐Ÿ”Œ ๐Ÿ”— ๐Ÿ†Ž, ๐Ÿ‘† ๐Ÿšฎ ๐Ÿ‘ซ โฌœ ๐Ÿ—œ:

from typing import List


def process_items(items: List[str]):
    for item in items:
        print(item)

๐Ÿ“ฃ ๐Ÿ”ข, โฎ๏ธ ๐ŸŽ โค (:) โ•.

๐Ÿ†Ž, ๐Ÿšฎ list.

๐Ÿ“‡ ๐Ÿ†Ž ๐Ÿ‘ˆ ๐Ÿ”Œ ๐Ÿ”— ๐Ÿ†Ž, ๐Ÿ‘† ๐Ÿšฎ ๐Ÿ‘ซ โฌœ ๐Ÿ—œ:

def process_items(items: list[str]):
    for item in items:
        print(item)

Info

๐Ÿ‘ˆ ๐Ÿ”— ๐Ÿ†Ž โฌœ ๐Ÿ—œ ๐Ÿค™ "๐Ÿ†Ž ๐Ÿ”ข".

๐Ÿ‘‰ ๐Ÿ’ผ, str ๐Ÿ†Ž ๐Ÿ”ข ๐Ÿšถโ€โ™€๏ธ List (โš–๏ธ list ๐Ÿ 3๏ธโƒฃ.9๏ธโƒฃ & ๐Ÿ”›).

๐Ÿ‘ˆ โ›“: "๐Ÿ”ข items list, & ๐Ÿ”  ๐Ÿฌ ๐Ÿ‘‰ ๐Ÿ“‡ str".

Tip

๐Ÿšฅ ๐Ÿ‘† โš™๏ธ ๐Ÿ 3๏ธโƒฃ.9๏ธโƒฃ โš–๏ธ ๐Ÿ”›, ๐Ÿ‘† ๐Ÿšซ โœ”๏ธ ๐Ÿ—„ List โšช๏ธโžก๏ธ typing, ๐Ÿ‘† ๐Ÿ’ช โš™๏ธ ๐ŸŽ ๐Ÿฅ” list ๐Ÿ†Ž โ†ฉ๏ธ.

๐Ÿ”จ ๐Ÿ‘ˆ, ๐Ÿ‘† ๐Ÿ‘จโ€๐ŸŽจ ๐Ÿ’ช ๐Ÿšš ๐Ÿ•โ€๐Ÿฆบ โช ๐Ÿญ ๐Ÿฌ โšช๏ธโžก๏ธ ๐Ÿ“‡:

๐Ÿต ๐Ÿ†Ž, ๐Ÿ‘ˆ ๐ŸŒ– ๐Ÿ’ช ๐Ÿ†.

๐Ÿ‘€ ๐Ÿ‘ˆ ๐Ÿ”ข item 1๏ธโƒฃ ๐Ÿ”ฃ ๐Ÿ“‡ items.

& , ๐Ÿ‘จโ€๐ŸŽจ ๐Ÿ’ญ โšซ๏ธ str, & ๐Ÿšš ๐Ÿ•โ€๐Ÿฆบ ๐Ÿ‘ˆ.

๐Ÿ”ข & โš’

๐Ÿ‘† ๐Ÿ”œ ๐ŸŽ ๐Ÿ“ฃ tupleโ“‚ & setโ“‚:

from typing import Set, Tuple


def process_items(items_t: Tuple[int, int, str], items_s: Set[bytes]):
    return items_t, items_s
def process_items(items_t: tuple[int, int, str], items_s: set[bytes]):
    return items_t, items_s

๐Ÿ‘‰ โ›“:

  • ๐Ÿ”ข items_t tuple โฎ๏ธ 3๏ธโƒฃ ๐Ÿฌ, int, โž•1๏ธโƒฃ int, & str.
  • ๐Ÿ”ข items_s set, & ๐Ÿ”  ๐Ÿšฎ ๐Ÿฌ ๐Ÿ†Ž bytes.

#๏ธโƒฃ

๐Ÿ”ฌ dict, ๐Ÿ‘† ๐Ÿšถโ€โ™€๏ธ 2๏ธโƒฃ ๐Ÿ†Ž ๐Ÿ”ข, ๐ŸŽ โ•.

๐Ÿฅ‡ ๐Ÿ†Ž ๐Ÿ”ข ๐Ÿ”‘ dict.

๐Ÿฅˆ ๐Ÿ†Ž ๐Ÿ”ข ๐Ÿ’ฒ dict:

from typing import Dict


def process_items(prices: Dict[str, float]):
    for item_name, item_price in prices.items():
        print(item_name)
        print(item_price)
def process_items(prices: dict[str, float]):
    for item_name, item_price in prices.items():
        print(item_name)
        print(item_price)

๐Ÿ‘‰ โ›“:

  • ๐Ÿ”ข prices dict:
    • ๐Ÿ”‘ ๐Ÿ‘‰ dict ๐Ÿ†Ž str (โžก๏ธ ๐Ÿ’ฌ, ๐Ÿ“› ๐Ÿ”  ๐Ÿฌ).
    • ๐Ÿ’ฒ ๐Ÿ‘‰ dict ๐Ÿ†Ž float (โžก๏ธ ๐Ÿ’ฌ, ๐Ÿ”– ๐Ÿ”  ๐Ÿฌ).

๐Ÿ‡ช๐Ÿ‡บ

๐Ÿ‘† ๐Ÿ’ช ๐Ÿ“ฃ ๐Ÿ‘ˆ ๐Ÿ”ข ๐Ÿ’ช ๐Ÿ™† ๐Ÿ“š ๐Ÿ†Ž, ๐Ÿ–ผ, int โš–๏ธ str.

๐Ÿ 3๏ธโƒฃ.6๏ธโƒฃ & ๐Ÿ”› (โœ… ๐Ÿ 3๏ธโƒฃ.1๏ธโƒฃ0๏ธโƒฃ) ๐Ÿ‘† ๐Ÿ’ช โš™๏ธ Union ๐Ÿ†Ž โšช๏ธโžก๏ธ typing & ๐Ÿšฎ ๐Ÿ”˜ โฌœ ๐Ÿ—œ ๐Ÿ’ช ๐Ÿ†Ž ๐Ÿšซ.

๐Ÿ 3๏ธโƒฃ.1๏ธโƒฃ0๏ธโƒฃ ๐Ÿ“ค ๐ŸŽ› โ• ๐ŸŒโ” ๐Ÿ‘† ๐Ÿ’ช ๐Ÿšฎ ๐Ÿ’ช ๐Ÿ†Ž ๐Ÿ‘ฝ โธ โธ (|).

from typing import Union


def process_item(item: Union[int, str]):
    print(item)
def process_item(item: int | str):
    print(item)

๐Ÿ‘ฏโ€โ™‚๏ธ ๐Ÿ’ผ ๐Ÿ‘‰ โ›“ ๐Ÿ‘ˆ item ๐Ÿ’ช int โš–๏ธ str.

๐ŸŽฒ None

๐Ÿ‘† ๐Ÿ’ช ๐Ÿ“ฃ ๐Ÿ‘ˆ ๐Ÿ’ฒ ๐Ÿ’ช โœ”๏ธ ๐Ÿ†Ž, ๐Ÿ’– str, โœ‹๏ธ ๐Ÿ‘ˆ โšซ๏ธ ๐Ÿ’ช None.

๐Ÿ 3๏ธโƒฃ.6๏ธโƒฃ & ๐Ÿ”› (โœ… ๐Ÿ 3๏ธโƒฃ.1๏ธโƒฃ0๏ธโƒฃ) ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ“ฃ โšซ๏ธ ๐Ÿญ & โš™๏ธ Optional โšช๏ธโžก๏ธ typing ๐Ÿ•น.

from typing import Optional


def say_hi(name: Optional[str] = None):
    if name is not None:
        print(f"Hey {name}!")
    else:
        print("Hello World")

โš™๏ธ Optional[str] โ†ฉ๏ธ str ๐Ÿ”œ โžก๏ธ ๐Ÿ‘จโ€๐ŸŽจ โ„น ๐Ÿ‘† ๐Ÿ” โŒ ๐ŸŒโ” ๐Ÿ‘† ๐Ÿ’ช ๐Ÿค” ๐Ÿ‘ˆ ๐Ÿ’ฒ ๐Ÿ•ง str, ๐Ÿ•โ” โšซ๏ธ ๐Ÿ’ช ๐Ÿค™ None ๐Ÿ’โ€โ™‚๏ธ.

Optional[Something] ๐Ÿค™ โŒจ Union[Something, None], ๐Ÿ‘ซ ๐ŸŒ“.

๐Ÿ‘‰ โ›“ ๐Ÿ‘ˆ ๐Ÿ 3๏ธโƒฃ.1๏ธโƒฃ0๏ธโƒฃ, ๐Ÿ‘† ๐Ÿ’ช โš™๏ธ Something | None:

from typing import Optional


def say_hi(name: Optional[str] = None):
    if name is not None:
        print(f"Hey {name}!")
    else:
        print("Hello World")
from typing import Union


def say_hi(name: Union[str, None] = None):
    if name is not None:
        print(f"Hey {name}!")
    else:
        print("Hello World")
def say_hi(name: str | None = None):
    if name is not None:
        print(f"Hey {name}!")
    else:
        print("Hello World")

โš™๏ธ Union โš–๏ธ Optional

๐Ÿšฅ ๐Ÿ‘† โš™๏ธ ๐Ÿ โฌ ๐Ÿ”› 3๏ธโƒฃ.1๏ธโƒฃ0๏ธโƒฃ, ๐Ÿ“ฅ ๐Ÿ’โ€โ™‚ โšช๏ธโžก๏ธ ๐Ÿ‘‡ ๐Ÿ“ถ ๐Ÿค” โ˜ ๐ŸŽ‘:

  • ๐Ÿ‘ถ โŽ โš™๏ธ Optional[SomeType]
  • โ†ฉ๏ธ ๐Ÿ‘ถ โš™๏ธ Union[SomeType, None] ๐Ÿ‘ถ.

๐Ÿ‘ฏโ€โ™‚๏ธ ๐ŸŒ“ & ๐Ÿ”˜ ๐Ÿ‘ซ ๐ŸŽ, โœ‹๏ธ ๐Ÿ‘ค ๐Ÿ”œ ๐Ÿ‘ Union โ†ฉ๏ธ Optional โ†ฉ๏ธ ๐Ÿ”ค "๐Ÿ“ฆ" ๐Ÿ”œ ๐Ÿ˜‘ ๐Ÿ”‘ ๐Ÿ‘ˆ ๐Ÿ’ฒ ๐Ÿ“ฆ, & โšซ๏ธ ๐Ÿค™ โ›“ "โšซ๏ธ ๐Ÿ’ช None", ๐Ÿšฅ โšซ๏ธ ๐Ÿšซ ๐Ÿ“ฆ & โœ”.

๐Ÿ‘ค ๐Ÿ’ญ Union[SomeType, None] ๐ŸŒ– ๐Ÿ”‘ ๐Ÿ”ƒ โšซ๏ธโ” โšซ๏ธ โ›“.

โšซ๏ธ ๐Ÿ”ƒ ๐Ÿ”ค & ๐Ÿ“›. โœ‹๏ธ ๐Ÿ‘ˆ ๐Ÿ”ค ๐Ÿ’ช ๐Ÿ“‰ โ” ๐Ÿ‘† & ๐Ÿ‘† ๐Ÿคฝโ€โ™‚ ๐Ÿ’ญ ๐Ÿ”ƒ ๐Ÿ“Ÿ.

๐Ÿ–ผ, โžก๏ธ โœŠ ๐Ÿ‘‰ ๐Ÿ”ข:

from typing import Optional


def say_hi(name: Optional[str]):
    print(f"Hey {name}!")

๐Ÿ”ข name ๐Ÿ”ฌ Optional[str], โœ‹๏ธ โšซ๏ธ ๐Ÿšซ ๐Ÿ“ฆ, ๐Ÿ‘† ๐Ÿšซ๐Ÿ”œ ๐Ÿค™ ๐Ÿ”ข ๐Ÿต ๐Ÿ”ข:

say_hi()  # Oh, no, this throws an error! ๐Ÿ˜ฑ

name ๐Ÿ”ข โœ” (๐Ÿšซ ๐Ÿ“ฆ) โ†ฉ๏ธ โšซ๏ธ ๐Ÿšซ โœ”๏ธ ๐Ÿ”ข ๐Ÿ’ฒ. , name ๐Ÿšซ None ๐Ÿ’ฒ:

say_hi(name=None)  # This works, None is valid ๐ŸŽ‰

๐Ÿ‘ ๐Ÿ“ฐ, ๐Ÿ• ๐Ÿ‘† ๐Ÿ”› ๐Ÿ 3๏ธโƒฃ.1๏ธโƒฃ0๏ธโƒฃ ๐Ÿ‘† ๐Ÿ† ๐Ÿšซ โœ”๏ธ ๐Ÿ˜Ÿ ๐Ÿ”ƒ ๐Ÿ‘ˆ, ๐Ÿ‘† ๐Ÿ”œ ๐Ÿ’ช ๐ŸŽฏ โš™๏ธ | ๐Ÿ”ฌ ๐Ÿ‡ช๐Ÿ‡บ ๐Ÿ†Ž:

def say_hi(name: str | None):
    print(f"Hey {name}!")

& โคด๏ธ ๐Ÿ‘† ๐Ÿ† ๐Ÿšซ โœ”๏ธ ๐Ÿ˜Ÿ ๐Ÿ”ƒ ๐Ÿ“› ๐Ÿ’– Optional & Union. ๐Ÿ‘ถ

๐Ÿ’Š ๐Ÿ†Ž

๐Ÿ‘‰ ๐Ÿ†Ž ๐Ÿ‘ˆ โœŠ ๐Ÿ†Ž ๐Ÿ”ข โฌœ ๐Ÿ—œ ๐Ÿค™ ๐Ÿ’Š ๐Ÿ†Ž โš–๏ธ ๐Ÿ’Š, ๐Ÿ–ผ:

  • List
  • Tuple
  • Set
  • Dict
  • Union
  • Optional
  • ...& ๐ŸŽ.

๐Ÿ‘† ๐Ÿ’ช โš™๏ธ ๐ŸŽ ๐Ÿ’ฝ ๐Ÿ†Ž ๐Ÿ’Š (โฎ๏ธ โฌœ ๐Ÿ—œ & ๐Ÿ†Ž ๐Ÿ”˜):

  • list
  • tuple
  • set
  • dict

& ๐ŸŽ โฎ๏ธ ๐Ÿ 3๏ธโƒฃ.6๏ธโƒฃ, โšช๏ธโžก๏ธ typing ๐Ÿ•น:

  • Union
  • Optional
  • ...& ๐ŸŽ.

๐Ÿ‘† ๐Ÿ’ช โš™๏ธ ๐ŸŽ ๐Ÿ’ฝ ๐Ÿ†Ž ๐Ÿ’Š (โฎ๏ธ โฌœ ๐Ÿ—œ & ๐Ÿ†Ž ๐Ÿ”˜):

  • list
  • tuple
  • set
  • dict

& ๐ŸŽ โฎ๏ธ ๐Ÿ 3๏ธโƒฃ.6๏ธโƒฃ, โšช๏ธโžก๏ธ typing ๐Ÿ•น:

  • Union
  • Optional (๐ŸŽ โฎ๏ธ ๐Ÿ 3๏ธโƒฃ.6๏ธโƒฃ)
  • ...& ๐ŸŽ.

๐Ÿ 3๏ธโƒฃ.1๏ธโƒฃ0๏ธโƒฃ, ๐ŸŽ› โš™๏ธ ๐Ÿ’Š Union & Optional, ๐Ÿ‘† ๐Ÿ’ช โš™๏ธ โธ โธ (|) ๐Ÿ“ฃ ๐Ÿ‡ช๐Ÿ‡บ ๐Ÿ†Ž.

๐ŸŽ“ ๐Ÿ†Ž

๐Ÿ‘† ๐Ÿ’ช ๐Ÿ“ฃ ๐ŸŽ“ ๐Ÿ†Ž ๐Ÿ”ข.

โžก๏ธ ๐Ÿ’ฌ ๐Ÿ‘† โœ”๏ธ ๐ŸŽ“ Person, โฎ๏ธ ๐Ÿ“›:

class Person:
    def __init__(self, name: str):
        self.name = name


def get_person_name(one_person: Person):
    return one_person.name

โคด๏ธ ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ“ฃ ๐Ÿ”ข ๐Ÿ†Ž Person:

class Person:
    def __init__(self, name: str):
        self.name = name


def get_person_name(one_person: Person):
    return one_person.name

& โคด๏ธ, ๐Ÿ”„, ๐Ÿ‘† ๐Ÿคš ๐ŸŒ ๐Ÿ‘จโ€๐ŸŽจ ๐Ÿ•โ€๐Ÿฆบ:

Pydantic ๐Ÿท

Pydantic ๐Ÿ ๐Ÿ—ƒ ๐ŸŽญ ๐Ÿ“Š ๐Ÿ”ฌ.

๐Ÿ‘† ๐Ÿ“ฃ "๐Ÿ’ " ๐Ÿ’ฝ ๐ŸŽ“ โฎ๏ธ ๐Ÿ”ข.

& ๐Ÿ”  ๐Ÿ”ข โœ”๏ธ ๐Ÿ†Ž.

โคด๏ธ ๐Ÿ‘† โœ ๐Ÿ‘ ๐Ÿ‘ˆ ๐ŸŽ“ โฎ๏ธ ๐Ÿ’ฒ & โšซ๏ธ ๐Ÿ”œ โœ” ๐Ÿ’ฒ, ๐Ÿ—œ ๐Ÿ‘ซ โ˜‘ ๐Ÿ†Ž (๐Ÿšฅ ๐Ÿ‘ˆ ๐Ÿ’ผ) & ๐Ÿค ๐Ÿ‘† ๐ŸŽš โฎ๏ธ ๐ŸŒ ๐Ÿ’ฝ.

& ๐Ÿ‘† ๐Ÿคš ๐ŸŒ ๐Ÿ‘จโ€๐ŸŽจ ๐Ÿ•โ€๐Ÿฆบ โฎ๏ธ ๐Ÿ‘ˆ ๐Ÿ“‰ ๐ŸŽš.

๐Ÿ–ผ โšช๏ธโžก๏ธ ๐Ÿ›‚ Pydantic ๐Ÿฉบ:

from datetime import datetime
from typing import List, Union

from pydantic import BaseModel


class User(BaseModel):
    id: int
    name: str = "John Doe"
    signup_ts: Union[datetime, None] = None
    friends: List[int] = []


external_data = {
    "id": "123",
    "signup_ts": "2017-06-01 12:22",
    "friends": [1, "2", b"3"],
}
user = User(**external_data)
print(user)
# > User id=123 name='John Doe' signup_ts=datetime.datetime(2017, 6, 1, 12, 22) friends=[1, 2, 3]
print(user.id)
# > 123
from datetime import datetime
from typing import Union

from pydantic import BaseModel


class User(BaseModel):
    id: int
    name: str = "John Doe"
    signup_ts: Union[datetime, None] = None
    friends: list[int] = []


external_data = {
    "id": "123",
    "signup_ts": "2017-06-01 12:22",
    "friends": [1, "2", b"3"],
}
user = User(**external_data)
print(user)
# > User id=123 name='John Doe' signup_ts=datetime.datetime(2017, 6, 1, 12, 22) friends=[1, 2, 3]
print(user.id)
# > 123
from datetime import datetime

from pydantic import BaseModel


class User(BaseModel):
    id: int
    name: str = "John Doe"
    signup_ts: datetime | None = None
    friends: list[int] = []


external_data = {
    "id": "123",
    "signup_ts": "2017-06-01 12:22",
    "friends": [1, "2", b"3"],
}
user = User(**external_data)
print(user)
# > User id=123 name='John Doe' signup_ts=datetime.datetime(2017, 6, 1, 12, 22) friends=[1, 2, 3]
print(user.id)
# > 123

Info

๐Ÿ’ก ๐ŸŒ– ๐Ÿ”ƒ Pydantic, โœ… ๐Ÿšฎ ๐Ÿฉบ.

FastAPI ๐ŸŒ โš“๏ธ ๐Ÿ”› Pydantic.

๐Ÿ‘† ๐Ÿ”œ ๐Ÿ‘€ ๐Ÿ“š ๐ŸŒ… ๐ŸŒ ๐Ÿ‘‰ ๐Ÿ’ก ๐Ÿ”ฐ - ๐Ÿ‘ฉโ€๐Ÿ’ป ๐Ÿฆฎ.

Tip

Pydantic โœ”๏ธ ๐ŸŽ ๐ŸŽญ ๐Ÿ•โ” ๐Ÿ‘† โš™๏ธ Optional โš–๏ธ Union[Something, None] ๐Ÿต ๐Ÿ”ข ๐Ÿ’ฒ, ๐Ÿ‘† ๐Ÿ’ช โœ ๐ŸŒ… ๐Ÿ”ƒ โšซ๏ธ Pydantic ๐Ÿฉบ ๐Ÿ”ƒ โœ” ๐Ÿ“ฆ ๐Ÿ‘.

๐Ÿ†Ž ๐Ÿ”‘ FastAPI

FastAPI โœŠ ๐Ÿ“ˆ ๐Ÿ‘ซ ๐Ÿ†Ž ๐Ÿ”‘ ๐Ÿ“š ๐Ÿ‘œ.

โฎ๏ธ FastAPI ๐Ÿ‘† ๐Ÿ“ฃ ๐Ÿ”ข โฎ๏ธ ๐Ÿ†Ž ๐Ÿ”‘ & ๐Ÿ‘† ๐Ÿคš:

  • ๐Ÿ‘จโ€๐ŸŽจ ๐Ÿ•โ€๐Ÿฆบ.
  • ๐Ÿ†Ž โœ….

...and FastAPI uses the same declarations :

  • ๐Ÿ”ฌ ๐Ÿ“„: โšช๏ธโžก๏ธ ๐Ÿ“จ โžก ๐Ÿ”ข, ๐Ÿ”ข ๐Ÿ”ข, ๐ŸŽš, ๐Ÿ’ช, ๐Ÿ”—, โ™’๏ธ.
  • ๐Ÿ—œ ๐Ÿ’ฝ: โšช๏ธโžก๏ธ ๐Ÿ“จ ๐Ÿšš ๐Ÿ†Ž.
  • โœ” ๐Ÿ’ฝ: ๐Ÿ‘Ÿ โšช๏ธโžก๏ธ ๐Ÿ”  ๐Ÿ“จ:
    • ๐Ÿญ ๐Ÿง โŒ ๐Ÿ“จ ๐Ÿ‘ฉโ€๐Ÿ’ป ๐Ÿ•โ” ๐Ÿ“Š โŒ.
  • ๐Ÿ“„ ๐Ÿ› ๏ธ โš™๏ธ ๐Ÿ—„:
    • โ” โคด๏ธ โš™๏ธ ๐Ÿง ๐ŸŽ“ ๐Ÿงพ ๐Ÿ‘ฉโ€๐Ÿ’ป ๐Ÿ”ข.

๐Ÿ‘‰ 5๏ธโƒฃ๐Ÿ“† ๐ŸŒ ๐Ÿ”Š ๐Ÿ“. ๐Ÿšซ ๐Ÿ˜Ÿ. ๐Ÿ‘† ๐Ÿ”œ ๐Ÿ‘€ ๐ŸŒ ๐Ÿ‘‰ ๐ŸŽฏ ๐Ÿ”ฐ - ๐Ÿ‘ฉโ€๐Ÿ’ป ๐Ÿฆฎ.

โš  ๐Ÿ‘œ ๐Ÿ‘ˆ โš™๏ธ ๐Ÿฉ ๐Ÿ ๐Ÿ†Ž, ๐Ÿ‘ ๐Ÿฅ‰ (โ†ฉ๏ธ โŽ ๐ŸŒ– ๐ŸŽ“, ๐Ÿ‘จโ€๐ŸŽจ, โ™’๏ธ), FastAPI ๐Ÿ”œ ๐Ÿ“š ๐Ÿ‘ท ๐Ÿ‘†.

Info

๐Ÿšฅ ๐Ÿ‘† โช ๐Ÿšถ ๐Ÿ”˜ ๐ŸŒ ๐Ÿ”ฐ & ๐Ÿ‘Ÿ ๐Ÿ”™ ๐Ÿ‘€ ๐ŸŒ… ๐Ÿ”ƒ ๐Ÿ†Ž, ๐Ÿ‘ โ„น "๐ŸŽฎ ๐ŸŽผ" โšช๏ธโžก๏ธ mypy.