squirrelworks

Logic: Python Type Annotations

Acorn

Type Annotations in Python are optional metadata that specify the expected data type of variables, function parameters, and return values. While not enforced at runtime by Python itself, they are essential for code clarity and automated linting.

In the example VERSION: Final[str] = '1.0.12', the variable name is VERSION. The type annotation segment is : Final[str]. This informs the developer (and the IDE) that this variable is intended to be a constant string and should not be modified.

Common Type Annotation Examples
Code Segment Data Type Functional Intent
PI: Final[float] Float / Constant Defines a constant numerical value with decimal precision.
brand: str String Expects text-based data for identification.
horsepower: int Integer Expects whole numbers for mathematical operations.
turned_on: bool Boolean Tracks binary state (True/False) for logic branching.

Implementation: Object-Oriented Programming

The following labs demonstrate class construction, initialization (__init__), and the implementation of dunder methods like __str__ and __add__.

Lab: The Microwave Class
class Microwave:
    def __init__(self, brand: str, power_rating: str) -> None:
            self.brand = brand
            self.power_rating = power_rating
            self.turned_on: bool = False

    def turn_on(self) -> None:
        if self.turned_on:
             print(f'Microwave ({self.brand}) is already turned on')
        else:
             self.turned_on = True
             print(f'Microwave ({self.brand}) is now turned on') 

    def run(self, seconds: int) -> None:
        if self.turned_on:
            print(f'Running ({self.brand}) for {seconds} seconds')
        else:
            print(f"must power on first")

    def __str__(self):
        return f'{self.brand} (Rating: {self.power_rating})'
Lab: The Car Class (Operator Overloading)
class Car:
    def __init__(self, brand: str, horsepower: int) -> None:
        self.brand = brand
        self.horsepower = horsepower

    def __str__(self) -> str:
        return f'{self.brand}, {self.horsepower}hp'
   
    def __add__(self, other) -> str:
        return f'{self.brand} & {other.brand}'
    
volvo: Car = Car('Volvo', 200)
bmw: Car = Car('BMW', 240)
print(volvo) # Output: Volvo, 200hp
print(volvo + bmw) # Output: Volvo & BMW
Execution Ledger: Verified Python Operations

This ledger recaps the specific Python 3.x logic and OOP structures verified during these lab sessions.

Domain Verified Action / Command Functional Result
OOP Class Instantiation Successfully created unique objects from the Microwave and Car templates.
OOP Dunder Method Implementation Verified __add__ and __str__ functionality for custom object behavior.
Typing Logic Audit: Type Annotations Verified syntax for optional type hinting and the Final type alias.

Also see: Java demo →



Accessibility
 --overview

Agile
 --DevOps overview
 --Principles

API
 --REST best practices
 --REST demo
 --REST vs RPC
 --Wikipedia API

Blockchain
 --overview

Cloud
 --AWS overview

CSS/HTML
 --Bootstrap carousel
 --Grid demo
 --markdown demo

Electricity
 --fundamentals

Encoding
 --Overview

Ergonomics
 --Desk configuration
 --Device fleet
 --Input device array
 --keystroke mechanics
 --Phones & RSI

ERP
 --Anthology overview
 --Ellucian Banner
 --Higher Ed ERP Simulation Lab
 --PeopleSoft Campus Solutions
 --PESC standards
 --Slate data model

Git
 --syntax overview
 --troubleshooting libcrypto

Hardware
 --Device fleet
 --Homelab diagram

Java
 --Fundamentals

Javascript
 --Advanced Interaction: jQuery & UI Frameworks
 --input prompt demo
 --misc demo
 --Time and Date functions
 --Vue demo

Linux
 --grep demo
 --HCI and Proxmox
 --Proxmox install
 --xammp ftp server

Mail flow
 --DKIM, SPF, DMARC
 --MAPI

Microsoft
 --AZ-800: Administering Windows Server Hybrid Core Infrastructure
 --BAT scripting
 --Group Policy
 --IIS
 --robocopy
 --Server 2022 setup - Virtualbox

Misc
 --Applications
 --regex
 --Resources
 --Sustainable Computing
 --Terminology
 --Tribute to Computer Scientists

Networks
 --BGP Peering & Security Hardening Lab
 --CCNA Lammle Study Guide
 --Cisco 1921/K9 router
 --routing protocols
 --throughput calculations

PHP/SQL
 --Cookies
 --database interaction
 --demo, OSI Layers quiz
 --Foreign key constraint demo
 --fundamentals
 --MySQL and PHPmyAdmin setup
 --pagination
 --security
 --session variables
 --SQL fundamentals
 --structures
 --Tables display

Python
 --fundamentals

Security
 --Overview- GRC (Governance, Risk, and Compliance)
 --Security Blog
 --SSH fundamentals

Serialization
 --JSON demo
 --YAML demo