How to explicitly specify the type of the variable passed to the class constructor?
How do I explicitly specify the type of the transferred variable in the constructor?
def __init__(self, height):
how do I make it so that I knew exactly what height will go value of type fleet
Right in the arguments when declaring the function in any way. Only inside the function can writeheight = float(height)
santos89 answered on April 3rd 20 at 17:44
No way. Python doesn't know static typing. But you can make some approximations:
1. mypy + typing will make Python a semblance TypeScript
2. typing. Your IDE (if normal) will tell you that you will pass is not float. But the runtime of such checks, as in paragraph number 1
3. Raise an error if if not isinstance(height, float). The check only works in runtime IDE nothing podsvetit
sonya_Weimann83 answered on April 3rd 20 at 17:46
Make your kastelic:
def __init__(self, height: float):
if not isinstance(height, float):
raise Exception('the height is not float type!')