日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

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

作者:三爺帶你飛 ? 更新時(shí)間: 2022-04-06 編程語(yǔ)言

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

isinstance() 與 type() 區(qū)別:

type() 不會(huì)認(rèn)為子類是一種父類類型,不考慮繼承關(guān)系。
isinstance() 會(huì)認(rèn)為子類是一種父類類型,考慮繼承關(guān)系。
如果要判斷兩個(gè)類型是否相同推薦使用 isinstance()。

type函數(shù)結(jié)果舉例,主要有六大類:

1、標(biāo)準(zhǔn)數(shù)據(jù)類型。

2、module模塊類型:主要來(lái)源于模塊安裝并使用

3、type類型:主要來(lái)源于標(biāo)準(zhǔn)數(shù)據(jù)類型的類對(duì)象

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é)

原文鏈接:https://blog.csdn.net/hzblucky1314/article/details/122659446

欄目分類
最近更新