日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python type函数和isinstance函数区别 - Python零基础入门教程

發(fā)布時間:2024/9/27 python 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python type函数和isinstance函数区别 - Python零基础入门教程 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

目錄

  • 一.Python type 函數(shù)簡介
  • 二.Python isinstance 函數(shù)簡介
  • 三.Python type 函數(shù)和 isinstance 函數(shù)區(qū)別
  • 四.猜你喜歡

零基礎(chǔ) Python 學習路線推薦 : Python 學習目錄 >> Python 基礎(chǔ)入門

Python 變量,也稱 Python 數(shù)據(jù)類型。Python 變量一共六種類型:整數(shù)/浮點數(shù)/字符串/BOOL/列表/元組/字典;

一.Python type 函數(shù)簡介

**Python 內(nèi)置函數(shù) type,該函數(shù)主要用于解析判斷 Python 變量類型;**type 函數(shù)語法如下:

''' 函數(shù)描述:type 函數(shù)用于獲取變量類型; 參數(shù):object : 實例對象; 返回值:直接或者間接類名、基本類型; ''' type(object)

二.Python isinstance 函數(shù)簡介

isinstance 函數(shù)是 **Python **中的一個內(nèi)置函數(shù),主要用于檢測變量類型,返回值是 bool 值 ,isinstance 函數(shù)語法如下:

''' 函數(shù)描述:主要用于檢測變量類型,返回值是 bool 值 參數(shù):object : 實例對象。classinfo : 可以是直接或者間接類名、基本類型或者由它們組成的元組。 返回值:如果對象的類型與classinfo類型相同則返回 True,否則返回 False。 '''isinstance(object,classinfo)

三.Python type 函數(shù)和 isinstance 函數(shù)區(qū)別

  • ** isinstance 函數(shù)會認為子類是一種父類類型,考慮繼承關(guān)系。**
  • ** type 函數(shù)不會認為子類是一種父類類型,不考慮繼承關(guān)系。**
# !usr/bin/env python# -_- coding:utf-8 \__-"""@Author:猿說編程@Blog(個人博客地址): www.codersrc.com@File:python type 函數(shù)和 isinstance 函數(shù)區(qū)別.py@Time:2021/3/21 11:37@Motto:不積跬步無以至千里,不積小流無以成江海,程序人生的精彩需要堅持不懈地積累!"""class Animation:passclass Dog(Animation):passprint(isinstance(Animation(), Animation)) # returns Trueprint(type(Animation()) == Animation) # returns Trueprint(isinstance(Dog(), Animation)) # returns Trueprint(type(Dog()) == Animation) # returns False'''輸出結(jié)果:TrueTrueTrueFalse'''

代碼分析

創(chuàng)建一個 Animation 對象,再創(chuàng)建一個繼承 Animation 對象的 Dog 對象,使用 isinstance 和 type 來比較 Animation 和 Animation 時,由于它們的類型都是一樣的,所以都返回了 True。

而 Dog 對象繼承于 Animation 對象,在使用 isinstance 函數(shù)來比較 Dog 和 Animation 時,由于考慮了繼承關(guān)系,所以返回了 True,使用 type 函數(shù)來比較 Dog 和 Animation 時,不會考慮 Dog 繼承自哪里,所以返回了 False

** 總結(jié):如果要判斷兩個類型是否相同,則推薦使用 isinstance 函數(shù)**;

四.猜你喜歡

  • Python 簡介
  • Python Pycharm Anacanda 區(qū)別
  • Python2.x 和 Python3.x,如何選擇?
  • Python 配置環(huán)境
  • Python Hello World 入門
  • Python 代碼注釋
  • Python 中文編碼
  • Anaconda 是什么?Anconda 下載安裝教程
  • Pycharm 提示:this license **** has been cancelled
  • Pycharm 設(shè)置開發(fā)模板/字體大小/背景顏色
  • 未經(jīng)允許不得轉(zhuǎn)載:猿說編程 ? Python type 函數(shù)和 isinstance 函數(shù)區(qū)別

    總結(jié)

    以上是生活随笔為你收集整理的Python type函数和isinstance函数区别 - Python零基础入门教程的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。