Day-15对象属性修改

1.getattribute

class Naither():
    def __init__(self):
        self.name = "老干妈"
        self.age = 60
    def __getattribute__(self, item):
        if item == "name":
            res = super().__getattribute__(item)
            return res[0]+"*"+res[-1]
        else:
            return super().__getattribute__(item)
# 实例化
laoganma = Naither()
print(laoganma.name)
print(laoganma.age)

2.getattr

class Sunther():
    def __init__(self):
        self.name = "孙贼,别跑!"
        self.age = 100
    def __getattr__(self, item):
        if "name" in item:
            return self.name
        elif "age" in item:
            return self.age
# 实例化
sunquan = Sunther()
print(sunquan.names)
print(sunquan.ages)

3.setattr

class S_Nvther():
    def __setattr__(self, key, value):
        if key == "name":
            pass
        else:
            super().__setattr__(key, value)
# 实例化
luoli = S_Nvther()
luoli.sex = "孙女乖乖"
# luoli.name = "小兔纸"
print(luoli.sex)
print(luoli.__dict__)

4.delattr

class MySelf():
    def __init__(self):
        self.name = "帅气的文广哥哥,长腿欧巴,颜值男神"
        self.girlfriend = "刘亦菲"
    def __delattr__(self, item):
        if item == "name":
            pass
        else:
            object.__delattr__(self, item)
# 实例化
wenguang = MySelf()
print(wenguang.__dict__)
del wenguang.girlfriend
print(wenguang.__dict__)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值