網站首頁 編程語言 正文
簡介:type() 函數可以對數據的類型進行判定。
isinstance() 與 type() 區別:
type() 不會認為子類是一種父類類型,不考慮繼承關系。 isinstance() 會認為子類是一種父類類型,考慮繼承關系。 如果要判斷兩個類型是否相同推薦使用 isinstance()。
type函數結果舉例,主要有六大類:
1、標準數據類型。
2、module模塊類型:主要來源于模塊安裝并使用
3、type類型:主要來源于標準數據類型的類對象
4、程序員新增的類,自定義的類型:<class ‘main.XXX’>、NoneType
5、builtin_function_or_method 內置函數或者方法
6、其他拓展類型如:collections.Counter、collections.deque等
源碼:
import time import random import asyncio import collections # 基本數據類型 print(type(1)) # <class 'int'> print(type(3.14)) # <class 'float'> print(type("hello")) # <class 'str'> print(type([1, 2])) # <class 'list'> print(type((1, "a"))) # <class 'tuple'> print(type({"name": "tom"})) # <class 'dict'> print(type(False)) # <class 'bool'> print("*" * 30) # <class 'module'> print(type(time)) print(type(random)) print(type(asyncio)) print("*" * 30) # <class 'type'> print(type(type)) print(type(int)) print(type(float)) print(type(bool)) print(type(str)) print(type(dict)) print(type(list)) print(type(tuple)) print(type(set)) print("*" * 30) # 自定義的類型:<class '__main__.XXX'> class A: x = 111 def __init__(self): self.x = 1 def run(self): pass @staticmethod def say(): pass @classmethod def live(cls): pass @property def sleep(self): pass a = A() print(type(A)) # <class 'type'> print(type(object)) # <class 'type'> print(type(a)) # <class '__main__.A'> # <class 'NoneType'> print(type(a.__init__())) print(type(a.run())) print(type(a.say())) print(type(a.live())) print(type(a.sleep)) print(type(A.x)) # <class 'int'> 與初始值類型一致 print(type(a.x)) # <class 'int'> 與初始值類型一致 print("*" * 30) # <class 'builtin_function_or_method'> print(type(None)) print(type(bin)) print(type(len)) print(type(min)) print(type(dir)) print("*" * 30) data = "message" result = collections.Counter(data) dict1 = collections.OrderedDict({"name": "Tom", "age": 25, "address": "CN"}) deq1 = collections.deque("abc") print(type(result)) # <class 'collections.Counter'> print(type(dict1)) # <class 'collections.OrderedDict'> print(type(deq1)) # <class 'collections.deque'>
實際應用舉例:
1、判定是否是lambda類型
2、判定是否是函數類型
3、判定是否是方法
4、判定生成器類型等
源碼:
from types import LambdaType, MethodType, GeneratorType, FunctionType, BuiltinFunctionType test1 = lambda x: x + 1 # 判定是否是lambda類型。需要注意的是lambda就是函數類型,本質是一樣的 print(type(test1) == LambdaType) # True # 判定是否是函數類型 print(type(test1) == FunctionType) # True # 判定是否是內置函數類型 print(type(bin) == BuiltinFunctionType) # True class Test2: def run(self): pass test2 = Test2() # 判定是否是方法 print(type(test2.run) == MethodType) # 判定生成器類型 a = (x * x for x in range(1, 10)) print(type(a) == GeneratorType)
總結
原文鏈接:https://blog.csdn.net/hzblucky1314/article/details/122659446
相關推薦
- 2022-10-29 Golang?動態腳本調研詳解_Golang
- 2022-09-17 C++中cin>>n的返回值_C 語言
- 2022-08-02 Android中TextView自動適配文本大小的幾種解決方案_Android
- 2022-03-16 .NET6導入和導出EXCEL_實用技巧
- 2022-09-20 以SortedList為例詳解Python的defaultdict對象使用自定義類型的方法_pyth
- 2022-04-09 C語言實現計算器的兩種方法_C 語言
- 2022-05-18 Python實現批量自動整理文件_python
- 2022-09-18 iOS?xcconfig編寫示例教程_IOS
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支