获取对象的信息

1.type()

代码演示:

import  types,os
#1.type()
#1.1获取类型
print(type("123"))
print(type(23))
print(type(True))
print(type(None))

#注意:type返回的是对应的Class类型
print(type(type("123")))   #<class 'type'>

"""
<class 'str'>
<class 'int'>
<class 'bool'>
"""

#1.2比较
print(type(123) == type(56))
print(type("123") == type(123))
print(type(True) == type(False))

print(type(123) == int)
print(type("abc") == str)
print(type(True) == bool)

#1.3判断一个对象是否是函数
#借助于types模块
print(type(abs) == types.BuiltinFunctionType)   #是否是系统内置的函数
print(type((x for x in range(1,10))) == types.GeneratorType)   #是否是生成器
print(type(lambda  x : x) == types.LambdaType)  #是否是匿名函数

def func():
    pass
print(type(func) == types.FunctionType)  #是否是自定义函数

 

2.isinstance()

代码演示:

#2.isinstance();判断一个对象是否属于某种数据类型
print(isinstance(3,int))
print(isinstance("abc",str))

#注意:判断某个对象是否属于元组中的其中一种数据类型    或
print(isinstance([1,2,3],(list,tuple)))
print(isinstance((1,2,3),(list,tuple)))

 

3.dir()

代码演示:

#3.dir():获取任意对象的所有信息,包含从父类中继承的内容【属性和方法】
print(dir(os))
print(dir("abc"))

"""
在Python中.变量的前后各有两个下划线,是有特殊用途的,比如__len__是len()在底层存在的形式

"""
print(len("abc"))
print("abc".__len__())

class Check(object):
    nanme = "gagj"
    def show(self):
        pass

c = Check()
print(dir(c))

 

posted @ 2018-11-22 13:31  SameSmile  阅读(325)  评论(0编辑  收藏  举报