Python的type函數(shù)結(jié)果你知道嘛
簡介:type() 函數(shù)可以對數(shù)據(jù)的類型進(jìn)行判定。
isinstance() 與 type() 區(qū)別:
type() 不會認(rèn)為子類是一種父類類型,不考慮繼承關(guān)系。 isinstance() 會認(rèn)為子類是一種父類類型,考慮繼承關(guān)系。 如果要判斷兩個類型是否相同推薦使用 isinstance()。
type函數(shù)結(jié)果舉例,主要有六大類:
1、標(biāo)準(zhǔn)數(shù)據(jù)類型。
2、module模塊類型:主要來源于模塊安裝并使用
3、type類型:主要來源于標(biāo)準(zhǔn)數(shù)據(jù)類型的類對象
4、程序員新增的類,自定義的類型:<class ‘main.XXX’>、NoneType
5、builtin_function_or_method 內(nèi)置函數(shù)或者方法
6、其他拓展類型如:collections.Counter、collections.deque等
源碼:
import time
import random
import asyncio
import collections
# 基本數(shù)據(jù)類型
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'>
實(shí)際應(yīng)用舉例:
1、判定是否是lambda類型
2、判定是否是函數(shù)類型
3、判定是否是方法
4、判定生成器類型等
源碼:
from types import LambdaType, MethodType, GeneratorType, FunctionType, BuiltinFunctionType
test1 = lambda x: x + 1
# 判定是否是lambda類型。需要注意的是lambda就是函數(shù)類型,本質(zhì)是一樣的
print(type(test1) == LambdaType) # True
# 判定是否是函數(shù)類型
print(type(test1) == FunctionType) # True
# 判定是否是內(nèi)置函數(shù)類型
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)
總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
對python中arange()和linspace()的區(qū)別說明
這篇文章主要介紹了對python中arange()和linspace()的區(qū)別說明,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05
python Pandas中數(shù)據(jù)的合并與分組聚合
大家好,本篇文章主要講的是python Pandas中數(shù)據(jù)的合并與分組聚合,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下2022-01-01
使用python搭建服務(wù)器并實(shí)現(xiàn)Android端與之通信的方法
今天小編就為大家分享一篇使用python搭建服務(wù)器并實(shí)現(xiàn)Android端與之通信的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06
Python中import導(dǎo)入上一級目錄模塊及循環(huán)import問題的解決
Python中的import語句導(dǎo)入模塊相信大家再熟悉不過了,這里我們會來講解Python中import導(dǎo)入上一級目錄模塊及循環(huán)import問題的解決,需要的朋友可以參考下2016-06-06
python利用TextBlob庫實(shí)現(xiàn)輕松分析文本情感
TextBlob是一個基于NLTK(Natural Language Toolkit)和Pattern庫的Python庫,它提供了一系列方便的接口和方法來處理文本數(shù)據(jù),下面我們就來學(xué)習(xí)一下如何利用TextBlob庫實(shí)現(xiàn)輕松分析文本情感吧2023-12-12
Python3實(shí)現(xiàn)的畫圖及加載圖片動畫效果示例
這篇文章主要介紹了Python3實(shí)現(xiàn)的畫圖及加載圖片動畫效果,結(jié)合實(shí)例形式分析了Python3基于tkinter庫進(jìn)行圖片加載動畫效果的相關(guān)實(shí)現(xiàn)與使用技巧,需要的朋友可以參考下2018-01-01
python 隨機(jī)密碼生成器的實(shí)現(xiàn)示例
隨機(jī)密碼生成器是一種非常有用的工具,它可以幫助我們生成隨機(jī)的、復(fù)雜的密碼,提高我們的賬戶安全性,本文就來介紹一下python 隨機(jī)密碼生成器的實(shí)現(xiàn)示例,感興趣的可以了解一下2023-11-11
drf-router和authenticate認(rèn)證源碼分析
在 Rest Framework 中提供了兩個 router , 可以幫助我們快速的實(shí)現(xiàn)路由的自動生成,本文通過實(shí)例代碼給大家介紹drf-router和authenticate認(rèn)證源碼分析,感興趣的朋友跟隨小編一起看看吧2021-07-07

