This article mainly introduces in detail how Python performs type checking on instance attributes. It has certain reference value. Interested friends can refer to it. I hope it can help you.
Case:
In a certain project, we implemented some classes and hoped to type-check their instance properties like static languages
p = Person()
p.name = 'xi_xi' # Must be str
p.age = 18 # Must be int
p.height = 1.75 # Must be float
Requirements:
You can specify the type for the instance variable name
Giving an incorrect type throws an exception
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
|
Related recommendations:
JavaScript type checking and internal properties [[Class]]
The above is the detailed content of Python implements type checking of instance attributes. For more information, please follow other related articles on the PHP Chinese website!