python type
type() 用于获取对象类型的。

  1. type(object) -> the object's type

测试

  1. class ClassTest:
  2. # declare a integer type variable
  3. var_int = 12
  4. # declare a string type variable
  5. var_str = 'monday'
  6. # declare a tuple type variable
  7. var_tup = (1, 2, 'mono')
  8. # it will show the ClassTest as a 'type'
  9. print('Type of ClassTest :', type(ClassTest))
  10. ct = ClassTest
  11. # it will show the ClassTest as a 'class'
  12. print('Type of ct :', type(ct))
  13. # it will print the type of var_int as 'int'
  14. print('Type of var_int :', type(ct.var_int))
  15. # it will print the type of var_str as 'str'
  16. print('Type of var_str :', type(ct.var_str))
  17. # it will print the type of var_tup as 'tuple'
  18. print('Type of var_tup :', type(ct.var_tup))

功能

基本上 type() 用于测试目的。

参考

https://www.journaldev.com/15076/python-type