Skip to content

Commit b6d8a1c

Browse files
committed
Update the type hints examples
1 parent e7b78be commit b6d8a1c

File tree

3 files changed

+53
-17
lines changed

3 files changed

+53
-17
lines changed
Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
11
# Annotations for Python Functions
22
# ------------------------------------------------------------------------------
3-
# Annotations in Python functions allow you to specify the expected types of
4-
# parameters and the return type of the function. This can help with code
5-
# readability and static type checking.
3+
# Annotations were introduced in Python 3.0 and allow developers to attach
4+
# metadata to function parameters and return values. The metadata can be of
5+
# any type, but it is commonly used to indicate the expected data types of
6+
# parameters and the return value of a function.
67
#
7-
# There are no strict rules enforced by Python regarding these A13_type_hints,
8-
# but they serve as a guide for developers and can be used by tools like
9-
# type checkers, IDEs, and documentation 01_generators.
8+
# Annotations are not enforced at runtime, but they can be used by static
9+
# type checkers, IDEs, and documentation generators to provide better
10+
# support for developers. The annotations are stored in the `__annotations__`
11+
# attribute of the function object.
12+
1013

1114
def add(x: int, y: int) -> int:
1215
return x + y
16+
17+
def sub(x: "int", y: "int") -> "int":
18+
return x - y
19+
20+
print(add.__annotations__)
21+
# Output: {'x': <class 'int'>, 'y': <class 'int'>, 'return': <class 'int'>}
22+
23+
print(sub.__annotations__)
24+
# Output: {'x': 'int', 'y': 'int', 'return': 'int'}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# The typing module provides support for type hints in Python.
2+
# ------------------------------------------------------------------------------
3+
# The `typing` was introduced in Python 3.5 and provides a convention on how to
4+
# use type hints in Python. In this regard, it provides a stricter naming
5+
# convention for type hints compared to simple annotations.
6+
#
7+
# The `typing` module provides a set of classes and functions that can be used
8+
# to define complex data types, such as lists, dictionaries, and tuples, with
9+
# specific types for their elements.
10+
11+
from typing import Union, Final
12+
13+
# Define a constant
14+
var: Final[int] = 100
15+
16+
# Define a function accepting multiple types for processing data
17+
def process_data(data: Union[int, float, str]) -> str:
18+
19+
if isinstance(data, (int, float)):
20+
return f"Numeric data processed: {data * 2}"
21+
22+
elif isinstance(data, str):
23+
return f"String data processed: {data.upper()}"
24+
25+
else:
26+
return "Unsupported data type"
27+
28+
print(process_data(10))
29+
# Output: Numeric data processed: 20
30+
31+
print(process_data(3.14))
32+
# Output: Numeric data processed: 6.28
33+
34+
print(process_data("hello"))
35+
# Output: String data processed: HELLO

examples/A13_type_hints/02_variable_annotations.py

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)