欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python的type函數(shù)結(jié)果你知道嘛

 更新時間:2022年01月24日 14:59:36   作者:三爺帶你飛  
這篇文章主要為大家介紹了Python的type函數(shù)結(jié)果,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助

簡介:type() 函數(shù)可以對數(shù)據(jù)的類型進行判定。

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'>

實際應(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)文章

最新評論