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.