# Python Fundamentals

> Explain and teach Python fundamentals with clear examples. Use this whenever the user asks about Python basics like variables, types, strings, lists/dicts/sets/

- Skill: `airs-general/python-fundamentals` (Agent Skill, multi-file: 2 files)
- Install (CLI): `npx get-skill add airs-general/python-fundamentals`
- Raw SKILL.md: https://api.skillmd.com/api/skills/airs-general/python-fundamentals/raw
- Safety review: pending
- Works with: any agent that reads SKILL.md (Claude Code, Claude.ai, Cursor, Codex, Windsurf, 60+ more)
- Category: Coding & Dev Tools
- Author: AIRS-general (https://skillmd.com/u/airs-general)
- Updated: 2026-12-05
- Page: https://skillmd.com/skills/airs-general/python-fundamentals

---

---
name: python-fundamentals
description: Explain and teach Python fundamentals with clear examples. Use this whenever the user asks about Python basics like variables, types, strings, lists/dicts/sets/tuples, functions, classes, modules, imports, virtual environments, exceptions, typing, async/await, iterators/generators, context managers, file I/O, and common standard library patterns.
---

You are a Python tutor and pair programmer. Prioritize correctness, clarity, and practical examples.

## Typing

### Annotated

`Annotated` is a Python typing feature from Python (PEP 593) that lets you attach extra metadata to a type hint without changing the type itself.
```py
from typing import Annotated

value: Annotated[int, "extra info"]
```
- `int` -> the real type
- `"extra info"` -> metadata (ignored by Python runtime unless a framework uses it)

Why use Annotated?
Normally Python type hints can only express: `x: int`

But sometimes frameworks need more context:
- validation rules
- database constraints
- **API dependencies**
- serialization behavior

`Annotated` allows attaching that extra context cleanly.

Basic example:
```py
from typing import Annotated, get_type_hints

# Annotated example, Format: Annotated[BaseType, Metadata]
def process(value: Annotated[int, "Range[1-100]"]):
    pass

# Access the metadata
hints = get_type_hints(process, include_extras=True)
print(hints['value'].__metadata__)  # Output: ('Range[1-100]',)
```

### `from __future__ import annotations`

a very clean way to avoid circular import issues caused by type hints—and in modern Python, it’s often the best default choice.

## Context management

A context manger is a construct that lets you manage setup and cleanup automatically using the with statement.

```py
with open("file.txt") as f:
    data = f.read()
```
1. File is opened (setup)
2. Code inside with runs
3. File is automatically closed (cleanup)

Without context manager, you need to manually close the file:
```py
f = open("file.txt")
data = f.read()
f.close()
```
* easy to forget close()
* crashes can leak resources

A context manger implements two methods:
```py
__enter__()  # setup
__exit__()   # cleanup
```
Example:
```py
class MyContext:
    def __enter__(self):
        print("Setup")
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        print("Cleanup")
```
Usage:
```py
with MyContext():
    print("Running code")
```

### Using `contextlib`

A simpler way:
```py
from contextlib import contextmanager

@contextmanager
def my_context():
    print("Setup")
    yield
    print("Cleanup")
```

## Enum


## Teaching approach
- Start with a high-level explanation of the concept.
- Follow with 1–2 small examples that the user can run.
- Use idiomatic Python and prefer modern features (Python 3.10+ syntax) unless the user’s version is older.
- If the user provides code, explain it in detail: what it does, why it works, and what edge cases exist.

## Core topics to cover well
### Data types and collections
- Numbers, strings, booleans, `None`
- `list`, `tuple`, `dict`, `set` and common operations
- Comprehensions and unpacking

### Functions
- Parameters (positional/keyword/default/*args/**kwargs)
- Return values, scope, closures
- Docstrings and basic testing patterns

### Modules and packaging (basics)
- `import` patterns, module layout
- `__name__ == "__main__"` usage

### Exceptions
- `try/except/else/finally`
- Creating custom exceptions and raising with context

### Classes and OOP
- `__init__`, instance vs class attributes
- `@property`, `@classmethod`, `@staticmethod`
- Dataclasses for simple models

### Iteration and generators
- Iterables vs iterators
- `yield`, generator expressions

### Context managers
- `with` statements
- Writing simple context managers

### Async fundamentals
- `async def`, `await`, tasks, and when async is worth it

## Output expectations
- Prefer runnable snippets and short exercises when appropriate.
- Keep concept explanations concise unless the user asks for deeper detail.
- Avoid adding unrelated topics; answer what the user asked, then optionally offer one natural next step.

## Example prompts this skill should handle well
- “Explain list vs tuple with examples.”
- “What does *args and **kwargs mean?”
- “Why am I getting UnboundLocalError here?”
- “Show me how generators work and when to use them.”
- “Explain async/await like I’m new to it.”

## Running tests

`conftest.py`

`conftest.py` is a pytest hook file, not a normal importable module. Pytest discovers `tests/conftest.py` automatically and makes its fixtures available to tests, but you generally don’t import from it .

request those fixtures by naming them as function arguments:
```py
def test_xxx(settings):
    ...
```

## Ipython

For ipython related: like jupyter notebook, ipdb refer to [IPython](ref/IPython.md)

## pickle

`_pickle.PicklingError: Can't pickl ... <lambda>`

cannot serialize lambda functions or locally defined functions.

## static method

A `@staticmethod` in Python is a method that:
* belongs to the class namespace
* does not receive self
* does not receive cls
* behaves like **a regular function grouped inside a class**

```py
class MathUtils:
    @staticmethod
    def add(a, b):
        return a + b


result = MathUtils.add(3, 5)

print(result)
```

## `classmethod`

A `@classmethod` in Python is a method that receives the class itself as the first argument, usually named `cls`.
It is used when the method needs to:
* access class variables
* create class instances
* support inheritance-aware constructors

```py
class User:
    count = 0

    def __init__(self, name):
        self.name = name
        User.count += 1

    @classmethod
    def get_count(cls):
        return cls.count


print(User.get_count())
```

## `global`

The `global` keyword is used inside a function to indicate that a variable refers to a module-level (global) variable rather than creating a new local variable.

Without global
```py
count = 0

def increment():
    count = count + 1  # Error

increment()
```
This raises:
```
UnboundLocalError: local variable 'count' referenced before assignment
```
Because Python sees the assignment to count and treats it as a local variable.

With global
```py
count = 0

def increment():
    global count
    count += 1

increment()
print(count)  # 1
```

The global declaration tells Python to modify the module-level variable.

