Python type()、isinstance()、dir()

type()和isinstance()区别:

type()通常是用于基础类型判断,isinstance()用于class类型判断

print(type(123)) ----------> 
print(type("abc") == type("ABC")) ----------> True
class Animal(object):
    def __init__(self, name, num):
        self.__name = name
        self.__num = num

    def get_name(self):
        return self.__name

    def set_name(self, name):
        self.__name = name

    def printNum(self):
        print("%s有%s个" % (self.name, self.num))

class Dog(Animal):
    def printNum(self):
        print("狗有%s个" % self.num)

d = Dog("哈士奇", 88)
print(isinstance(d, Dog)) ----------> True
print(isinstance(d, Animal)) ----------> True

dir()用于获得一个对象的所有属性和方法

print(dir(d))
image.png

你可能感兴趣的:(Python type()、isinstance()、dir())