Skip to content

Commit eebbf88

Browse files
committed
Add example of user-defined class type hint
1 parent de8b801 commit eebbf88

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

source-code/typing/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,5 @@ Type checking can be done using [mypy](http://mypy-lang.org/index.html).
3636
false positive.
3737
1. `numpy_typing.py`: illustration of a script using both numpy and
3838
matplotlib with type hints.
39+
1. `classes.py`: illustration of using type hints with a user-defined
40+
class.

source-code/typing/classes.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env python
2+
3+
from typing import Any
4+
5+
6+
class MyClass:
7+
8+
def __init__(self, data: Any) -> None:
9+
self.data = data
10+
11+
@property
12+
def data(self) -> int:
13+
return self.data
14+
15+
@data.setter
16+
def data(self, value: Any) -> None:
17+
self.data = int(value)
18+
19+
def __repr__(self) -> str:
20+
return f'data: {self.data}'
21+
22+
23+
def print_data(data: MyClass) -> None:
24+
print(data)
25+
26+
27+
if __name__ == '__main__':
28+
data = MyClass('25')
29+
print_data(data)

0 commit comments

Comments
 (0)