Python 面向对象编程 OOP 05--类、实例和其他对象的内建函数

        

目   录

1. issubclass() 布尔函数,判断一个类是另外一个类的子类或者子孙类

2. isinstance() 布尔函数,判断一个对象是某个类的实例

3. hasattr()、getattr()、setattr()、delattr() 自省/反射函数,动态执行

3.1 对类进行相关操作

3.2 对实例进行相关操作 

3.2.1 不受限制的使用

3.2.2 使用 __slots__ 进行限制 

4. dir() 函数

4.1 作用在实例上

4.2 作用在类上

4.3 作用在模块上

4.4 不带参数时候

5. vars() 函数

其他文章:

Python 面向对象编程 OOP--01--类和实例的基本概念

Python 面向对象编程 OOP--02--类中的单下划线和双下划线 5 种情况

Python 面向对象编程 OOP--03--类的组合和派生

Python 面向对象编程 OOP--04--多重继承和方法解释顺序 MRO

Python 面向对象编程 OOP 05--类、实例和其他对象的内建函数

Python 面向对象编程 OOP--06--super() 是一个描述器类


        先准备代码,就着代码讲用法,就好像一边烤肉一边吃一样,很香。  

class Man():
    '''
    This class is used to describle human!
    '''
    def __init__(self,name,age):
        self.name=name
        self.age=age
        self.love="self"
    
    def work(self):
        print("Now a man is working")

class Teacher(Man):
    '''
    This class is used to describle worker!
    '''
    def __init__(self,name,age):
        # 调用父类的初始化方法 __init__
        super().__init__(name,age)
        self.love="Students"

    #重新定义 work 方法,这是派生
    def work(self):
        print("Now a teacher is teaching!")

class Singer(Man):
    '''
    This class is used to describle singer!
    '''
    def __init__(self,name,age,country):
        # 调用父类的初始化方法 __init__
        super().__init__(name,age)
        self.country=country
        self.love="Song"

    #重新定义 work 方法,这是派生
    def work(self,song):
        print("Now a singer is singing "+song+" !")
        
class SingerTeacher(Singer,Teacher):
    '''
    This class is used to describle singer  tearcher!
    '''
    #重新定义 work 方法,这是派生
    def work(self,song):
        print("Now a singer  tearcher is working !")
        
allen=SingerTeacher("allen",20,"China")

1. issubclass() 布尔函数,判断一个类是另外一个类的子类或者子孙类

print("SingerTeacher is a subclass of Singer?",issubclass(SingerTeacher,Singer))
print("SingerTeacher is a subclass of Teacher?",issubclass(SingerTeacher,Teacher))
print("SingerTeacher is a subclass of Man?",issubclass(SingerTeacher,Man))
print("Teacher is a subclass of Singer?",issubclass(Teacher,Singer))

2. isinstance() 布尔函数,判断一个对象是某个类的实例

print("allen is a instance of SingerTeacher?",isinstance(allen,SingerTeacher))
print("allen is a instance of Singer?",isinstance(allen,Singer))
print("allen is a instance of Teacher?",isinstance(allen,Teacher))
print("allen is a instance of Man?",isinstance(allen,Man))
print("0 is a instance of Man?",isinstance(0,Man))

3. hasattr()、getattr()、setattr()、delattr() 自省/反射函数,动态执行

3.1 对类进行相关操作

# 对类 进行操作
# hasattr()、getattr()、setattr()、delattr() 
print("1.SingerTeacher has attribute age?",hasattr(SingerTeacher,"age"))
try:
    print("2.SingerTeacher get attribute love?",getattr(SingerTeacher,"love"))
except AttributeError:
    print("2.SingerTeacher donot hvae attribute love")
    print("2.love 是实例属性,不是类属性")
try:
    print("3.Singer get attribute love?",getattr(Singer,"love"))
except AttributeError:
    print("3.Singer donot hvae attribute love")
    print("3.love 是实例属性,不是类属性")
print("4.SingerTeacher set new attribute comment?",setattr(SingerTeacher,'comment','This class is for who can sing and teach!'))
print("5.SingerTeacher get attribute comment==>",getattr(SingerTeacher,"comment"))
print("6.SingerTeacher delete attribute comment==>",delattr(SingerTeacher,"comment"))
print("7.SingerTeacher has attribute comment?",hasattr(SingerTeacher,"comment"))

 

3.2 对实例进行相关操作 

3.2.1 不受限制的使用

# 对实例进行操作
# hasattr()、getattr()、setattr()、delattr() 
print("1.allen has attribute age?",hasattr(allen,"age"))
try:
    print("2.allen get attribute love?",getattr(allen,"love"))
    print("2.love 是实例属性,不是类属性")
except AttributeError:
    print("2.allen donot hvae attribute love")
try:
    print("3.allen get attribute love?",getattr(allen,"love"))
    print("3.love 是实例属性,不是类属性")
except AttributeError:
    print("3.allen donot hvae attribute love")
print("4.allen set new attribute comment?",setattr(allen,'comment','This class is for who can sing and teach!'))
print("5.allen get attribute comment==>",getattr(allen,"comment"))
print("6.allen delete attribute comment==>",delattr(allen,"comment"))
print("7.allen has attribute comment?",hasattr(allen,"comment"))

3.2.2 使用 __slots__ 进行限制 

        __slots__ 是一个类变量,由一序列型对象组成,由所有合法标识组成的实例属性的集合来表示。它可以是一个列表,元组或者可迭代对象,也可以是标识实例能拥有的唯一的属性的简单字符串。

        任何企图创建一个不在 __slots__ 中的名字的实例属性,都将导致 AttributeError 异常。

class Man(object):
    '''
    This class is used to describle human!
    '''
    __slots__=("name","age","skin")
    def __init__(self,name,age):
        self.name=name
        self.age=age
    
    def work(self):
        print("Now a man is working")
allen=Man("allen",20)
allen.color="Black"

4. dir() 函数

4.1 作用在实例上

        dir() 函数作用在实例上,显示实例变量,还有实例所在类和所有父类中定义的方法和属性。

4.2 作用在类上

        dir() 函数作用在类上,显示类和所有父类中 __dict__中的内容。但是不会显示定义在元类 metaclass 中的类内容。

4.3 作用在模块上

        dir() 函数作用在模块上,显示模块中 __dict__中的内容。

4.4 不带参数时候

        dir() 函数不带参数时候,显示调用者的局部变量。

5. vars() 函数

        vars() 函数与 dir() 函数类似,只是给定的对象参数都必须有一个 __dict__ 属性。vars() 函数 返回一个字典,它包含了对象存储于其 __dict__ 中的属性(键)和值。如果对象没有 __dict__ 属性,则会导致一个 TypeError 异常。

        如果vars() 函数 不带参数,则显示一个包含本地名字空间的属性(键)和值的字典,也就是 locals()。

################### 求点赞,求转发,求评论,求收藏!#####################

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

江南野栀子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值