Chris Padilla/Blog

You can follow by Newsletter or RSS! (What's RSS?) Full archive here.

    Type Hinting in Python

    After spending some time in Java, TypeScript, and C#, I've started to miss typing when it's not there.

    While Python won't keep from compiling if there's a type issue, the autocomplete and clarity that comes with Type Hinting is a boon to productivity and clarity.

    The Basics

    A quick, simple example:

    def say_name(name: str) -> str:
        return "My name is " + name

    name: str is declaring that name is the built-in string type. The -> str denotes that we are returning a string.

    Not Fool-Proof

    Say that there are some conditions to the function:

    def say_name_if_starts_with_j(name: str) -> str:
        if name[0] is 'J':
            return "My name is " + name
            return False

    I've said this method will return a string, but in some cases it returns a boolean. In statically typed languages, this would raise an error at compile time, and/or my editor may alert me to the issue. In Python, however, these are only hints. So this code flies.

    Let's fix it before moving on:

    from typing import Union
    def say_name_if_starts_with_j(name: str) -> Union[str, bool]:
        if name[0] is 'J':
            return "My name is " + name
            return False

    Why It's Called Type Hinting

    The main benefit here comes later as I'm calling the function. Say I'm in the middle of writing this code:

    name = say_name_if_starts_with_j("Jake The Dog")
    # autocomplete suggests string methods, such as "capitalize"

    Because we've gone through the trouble of typing our return value, my code editor knows to suggest string methods (as well as boolean methods!)

    Hovering over the say_name_if_starts_with_j method now also includes a line recreating my defenition:

    (method) def say_name_if_starts_with_j(
        name: str
    ) -> str

    We're missing some of the compiling and linting benefits of statically typed languages out of the box with Type Hinting. However, it's a feature of the language that still has value in communication!

    Typing here allows for me to be intentional with my methods and allows for an opportunity to communicate clearly the expected inputs and outputs of my function. One less trip to another tab in my code editor or browser can go a long way for keeping the flow going.

    Foolin' With Funk

    Listen on Youtube

    Groovin' on a Sunday mornin'

    Sunset Over Campion Trail Creek


    Our favorite time to walk the trail 🌅

    Raspberry Snow Cone

    Listen on Youtube

    Recording a new album! Bit of a rock ballad vibe

    Vintage Computer Festival Southwest!

    I recently attended Vintage Computer Festival Southwest. It's a great, no frills celebration of the history and preservation of personal computing. Everything from Atari machines to the single body iMac G3's were present!

    We're pretty spoiled with technological advancement and availability over here in the US of A. It's easy to forget how phenomenal what we have is.

    If you haven't fooled with a classic computer in a few decaeds — it's an interesting cocktail of experiences! One part nostalgic, a dash of disorientation (how does this work again?), sometimes impressive for the time, and other times quaint.

    One thing that was consistent across the folks I chatted with: Everyone had a story where their eyes brightened, the conversation got spirited, and they could tap into an early memory of being enamored with what was possible with a machine. Be it putting a PC together, playing games, or programming.

    If you're really open to taking a step into your own early memories with these machines, there's a unique opportunity. It's a chance to connect with the spirit of why technology matters beyond its pragmatic function: to inspire awe and wonder.

    (If you're looking for a quick taste, I'd recommend LGR on YouTube. I could watch this man repair 90s PCs for days on end.)