python isinstance()函数和type()函数
一、type()用法
描述:
python的 type 函数有两个用法,当只有一个参数的时候,返回对象的类型。当有三个参数的时候返回一个类对象。
语法:
一个参数:type(object)
三个参数:type(name,bases,dict)
用法:
一个参数时,type()返回一个对象的数据类型
>>> type(1)
<class 'int'>
>>> type('alex')
<class 'str'>
>>> type([1,2,3])
<class 'list'>
>>> type((1,2,3))
<class 'tuple'>
>>> type({'zero':0,'one':1})
<class 'dict'>
>>> type(1) == int
True
>>>
三个参数时:
name:类名
bases: 父类的元组
dict: 类的属性方法和值组成的键值对
创建一个类
# 构造函数
def __init__(self, name):
self.name = name
# 实例(普通)方法
def instancetest(self):
print('this is instance method') # 类方法
@classmethod
def classtest(cls):
print('This is a class method') # 静态方法
@staticmethod
def statictest(n):
print('This is a static method %s' % n) #创建类
test_property = {'number': 1, '__init__':__init__,'instancetest1':instancetest,
'classtest': classtest, 'statictest': statictest}# 属性和方法
Test = type('Tom', (object,), test_property) # 实例化
test = Test('alex')
print(test.name)
print(test.number)
test.instancetest1()
test.classtest()
test.statictest(7)
执行结果:
alex
1
this is instance method
This is a class method
This is a static method 7
用help()打印Test的详细信息
class Tom(builtins.object)
| Tom(name)
|
| Methods defined here:
|
| __init__(self, name)
| # 构造函数
|
| instancetest1 = instancetest(self)
| # 实例(普通)方法
|
| ----------------------------------------------------------------------
| Class methods defined here:
|
| classtest() from builtins.type
| # 类方法
|
| ----------------------------------------------------------------------
| Static methods defined here:
|
| statictest(n)
| # 静态方法
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables (if defined)
|
| __weakref__
| list of weak references to the object (if defined)
|
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| number = 1
可以看出我们创建了一个Test类,包含一个实例方法包含一个构造方法__init__,实例方法statictest,类方法classtest,静态方法statictest1,和一个属性number =1。
注意:
Type和Object
type为对象的顶点,所有对象都创建自type。
object为类继承的顶点,所有类都继承自object。
python中万物皆对象,一个python对象可能拥有两个属性,__class__ 和 __base__,__class__ 表示这个对象是谁创建的,__base__ 表示一个类的父类是谁。
>>> object.__class__
<class 'type'>
>>> type.__base__
<class 'object'>
可以得出结论:
- type类继承自object
- object的对象创建自type
二、isinstance() 用法
描述:
判断一个对象时否来自一个已知类型
语法:
isinstance(object, classinfo)
参数:
- object -- 实例对象。
- classinfo -- 可以是直接或间接类名、基本类型或者由它们组成的元组。
返回值:
如果对象的类型与参数二的类型(classinfo)相同则返回 True,否则返回 False。
>>>a = 2
>>> isinstance (a,int)
True
>>> isinstance (a,str)
False
>>> isinstance (a,(str,int,list)) # 是元组中的一个返回 True
True
三、type()和isintance()函数的区别
isinstance() 与 type() 区别:
type() 不会认为子类是一种父类类型,不考虑继承关系。
isinstance() 会认为子类是一种父类类型,考虑继承关系。
如果要判断两个类型是否相同推荐使用 isinstance()。
class A(object):
pass
class B(A):
pass print(isinstance(A(), A))
print(isinstance(B(), A))
print(type(A()) == A)
print(type(B()) == A)
执行结果:
True
True
True
False
python isinstance()函数和type()函数的更多相关文章
- 《初识Python之认识常量type函数》
<初识Python之认识常量type函数> 1.2 认识常量 1.常量:我们用的就是它字面意义上的值或内容. 2.数字(Number) (1)整数表示:97. (2)浮点数表示:5.29 ...
- python中一些有用的函数------持续更新中
strip() 函数 用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列. str2 = " Runoob " # 去除首尾空格 print (str2.strip()) ...
- 【我要学python】MethodType和isinstance和Type函数
一.首先来看isinstance: a=6 isinstance(a,int) #返回Ture isinstance(a,str) #返回False isinstance (a,(str,int,li ...
- python---issubclass/type/isinstance/ 反射(内置函数getattr/delattr...)
# 一 python面向对象-内置函数(issubclass(), type(), isinstance()) # issubclass 判断xxxx类是否是xxxx类的子类 class egg: p ...
- python入门(二):isinstance、内置函数、常用运算等
1. isinstance(变量名,类型) #判断什么类型 ps: 只支持输入两个参数,输入3个参数会报错 >>> isin ...
- Python内置函数(43)——type
英文文档: class type(object) class type(name, bases, dict) With one argument, return the type of an obje ...
- Python内置函数(65)——type
英文文档: class type(object) class type(name, bases, dict) With one argument, return the type of an obje ...
- python之type函数
python 的type 函数 的介绍的 下面就是此函数的参数 三个参数的意义 '''type(class_name, base_class_tuple, attribute_dict)cla ...
- Python type() 函数
描述 type() 函数如果你只有第一个参数则返回对象的类型,三个参数返回新的类型对象.类似isinstance() isinstance() 与 type() 区别: type() 不会认为子类是一 ...
随机推荐
- elasticsearch 基础 —— Query String
使用查询解析器来解析其内容的查询.下面是一个例子: GET /_search { "query": { "query_string" : { "def ...
- 03.Linux-CentOS系统user用户改密码问题
问题:[user@localhost ~]$ passwdChanging password for user user.Changing password for user.(current) UN ...
- Kintex7 XC7K325T 板卡三剑客
(226)基于Xilinx Kintex-7 FPGA K7 XC7K325T PCIeX8 四路光纤卡 (227)基于Xilinx Kintex-7 FPGA K7 XC7K325T的FMC U ...
- Git--01 基础 - 远程仓库的使用
目录 Git 基础 - 远程仓库的使用 远程仓库的使用 查看远程仓库 添加远程仓库 从远程仓库中抓取与拉取 推送到远程仓库 查看某个远程仓库 远程仓库的移除与重命名 Git 基础 - 远程仓库的使用 ...
- Codecraft-17 and Codeforces Round #391 - A
题目链接:http://codeforces.com/contest/757/problem/A 题意:给定一个字符串,问你从这个字符串中选出一些字符然后重新排序后最多能组成多少个 Bulbasaur ...
- python常用函数 F
filter(callable, list/tuple) 接收一个函数和一个序列,完成元素过滤. 例子: fnmatch(str,str) 使用底层操作系统的大小写敏感规则来匹配模式. 例子: fnm ...
- 洛谷P1446/BZOJ1004 Cards Burnside引理+01背包
题意:有n张牌,有R+G+B=n的3种颜色及其数量,要求用这三种颜色去染n张牌.n张牌有m中洗牌方式,问在不同洗牌方式下本质相同的染色方案数. 解法:这道题非常有意思,题解参考Hzwer学长的.我这里 ...
- Kotlin搞起来——2.基础知识
在上一节中简单的给大家介绍了下Kotlin的特点,以及结合自己实际项目 中的使用来帮助大家了解这门语言,其实真的没你想象中的那么难,本文打算 介绍的是Kotlin中基础相关的一些语法(用法),有个大概 ...
- Vue-鼠标按键修饰符
left .right .middle 这些修饰符会限制处理函数仅响应特定的鼠标按钮. 如下例子 <div id="app"> <input type=" ...
- 【串线篇】Mybatis缓存之缓存查询顺序
1. 不会出现一级缓存和二级缓存中有同一个数据.因为二级缓存是在一级缓存关闭之后才有的 2.任何时候都是先看二级缓存.再看一级缓存,如果大家都没有就去查询数据库,数据库的查询后的结果放在一级缓存中了: ...