# Type Hints in Python

> Learned about this today, so I'm organizing it!
>
> Reference: [python docs](https://docs.python.org/3.8/library/typing.html)

\ <br>

## Before getting started

* The Python runtime does not enforce type annotations on functions or variables
  * You need to use an IDE or type checker to verify them
* You can provide type hints to indicate types when using methods

\ <br>

## Function Annotations

> [docs](https://www.python.org/dev/peps/pep-3107/)

* Function annotations can be used for **Parameters** and **return values**, but they are not mandatory!
* Function annotations can be used to provide type checking
  * However, Lambda does not support annotations!

<br>

### Syntax

#### 1. Parameters

ex1) **parameter**

```python
def foo(a: expression, b: expression = 2):
  ...
```

<br>

ex2) **excess parameter** ( `*args`, `**kwargs` )

```python
def foo(*args: expression, **kwargs: expression):
    ...
```

<br>

#### 2. Return Values

ex)

```python
def sum() -> expression:
  ...
```

\
\ <br>

`+`

## Type aliases

: Additionally, you can also use aliases!

ex)

```python
from typing import List
Vector = List[float]

def scale(scalar: float, vector: Vector) -> Vector:
    return [scalar * num for num in vector]

# typechecks; a list of floats qualifies as a Vector.
new_vector = scale(2.0, [1.0, -4.2, 5.4])
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://chloe-codes1.gitbook.io/til/python/python-101/type_hints_in_python.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
