ch25: new example with __init_subclas__
This commit is contained in:
21
25-class-metaprog/checked/checkedlib_demo.py
Normal file
21
25-class-metaprog/checked/checkedlib_demo.py
Normal file
@@ -0,0 +1,21 @@
|
||||
from checkedlib import Checked
|
||||
|
||||
class Movie(Checked):
|
||||
title: str
|
||||
year: int
|
||||
megabucks: float
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
movie = Movie(title='The Godfather', year=1972, megabucks=137)
|
||||
print(movie.title)
|
||||
print(movie)
|
||||
try:
|
||||
# remove the "type: ignore" comment to see Mypy error
|
||||
movie.year = 'MCMLXXII' # type: ignore
|
||||
except TypeError as e:
|
||||
print(e)
|
||||
try:
|
||||
blockbuster = Movie(title='Avatar', year=2009, megabucks='billions')
|
||||
except TypeError as e:
|
||||
print(e)
|
||||
Reference in New Issue
Block a user