class Foo:
x=1
def __init__(self,y):
self.y=y def __getattr__(self, item):
print('----> from getattr:你找的属性不存在') def __setattr__(self, key, value):
print('----> from setattr')
# self.key=value #这就无限递归了,你好好想想
# self.__dict__[key]=value #应该使用它 def __delattr__(self, item):
print('----> from delattr')
# del self.item #无限递归了
self.__dict__.pop(item) #__setattr__添加/修改属性会触发它的执行
f1=Foo(10)
print(f1.__dict__) # 因为你重写了__setattr__,凡是赋值操作都会触发它的运行,你啥都没写,就是根本没赋值,除非你直接操作属性字典,否则永远无法赋值
f1.z=3
print(f1.__dict__) #__delattr__删除属性的时候会触发
f1.__dict__['a']=3#我们可以直接修改属性字典,来完成添加/修改属性的操作
del f1.a
print(f1.__dict__) #__getattr__只有在使用点调用属性且属性不存在的时候才会触发
f1.xxxxxx 三者的用法演示
class Foo:
def __init__(self,x):
self.x = x def __getattr__(self, item):
print("执行__getattr__") def __delattr__(self, item):
print("执行 __delattr__")
self.__dict__.pop(item)
def __setattr__(self, key, value):
print("执行__setattr__")
self.__dict__[key] = value f1 = Foo(10) # 执行__setattr__
print(f1.x) #
f1.dfhsjdsfh # 执行__getattr__ 调用f1 中不存在的属性是会触发 print(f1.__dict__) # {'x': 10}
f1.y = 3 # 执行__setattr__
print(f1.__dict__) # {'y': 3, 'x': 10} f1.__dict__['z'] = 5
print(f1.__dict__) # {'x': 10, 'y': 3, 'z': 5}
del f1.z # 执行 __delattr__
print(f1.__dict__) # {'x': 10, 'y': 3}

面向对象——类的内置attr(三十三)的更多相关文章

  1. python - 类的内置 attr 方法

    类的内置 attr 方法 #类的内置 attr 方法: # __getattr__ # __setattr__ # __delattr__ # __getattr__ #到调用一个类不存在数参数时,将 ...

  2. python 面向对象 类的内置方法

    判断是不是类cls的对象 class A: pass a = A() print(isinstance(a,A)) 判断类sub是不是super的子类 class A: pass class B(A) ...

  3. python面向对象--类的内置函数

    #isinstance(obj,cls)判断obj是否是类cls的实例 #issubclass(cls,cls1)判断cls是否是cls1的子类或派生类 class Foo: pass class B ...

  4. python面向对象--类的内置方法

    #isinstance(obj,cls)判断obj是否是类cls的实例 #issubclass(cls,cls1)判断cls是否是cls1的子类或派生类 class Foo: pass class B ...

  5. python类的内置attr属性

    class Foo: x=1 def __init__(self,y): self.y=y def __getattr__(self, item): print('----> from geta ...

  6. 第三十五篇 类的内置属性(attr属性),包装和授权,__getattr__

    双下划线开头的attr方法,都是类内置的方法. 一. 如果没有在类里定义这三个方法,调用的时候就调用类内置的默认的方法 class Too: pass # 类没有定义这三个属性,就用系统默认的方法 t ...

  7. 多态,封装,反射,类内置attr属性,os操作复习

    1.多态 #多态 多态是指对象如何通过他们共同的属性和动作来操作及访问,而不需要考虑他们具体的类 运行时候,多种实现 反应运行时候状态 class H2O: def __init__(self,nam ...

  8. day28 面向对象:反射,内置函数,类的内置方法

    面向对象进阶博客地址链接: http://www.cnblogs.com/Eva-J/articles/7351812.html 复习昨日内容: # 包 # 开发规范 # # hashlib # 登录 ...

  9. python的反射函数(hasattr()、getattr()、setattr()与delattr())和类的内置属性attr(__getattr()__、__setattr()__与__delattr()__)

    主要是指程序可以访问.检测和修改它本身状态或行为的一种能力(自省),有四个可以实现自省函数. hasattr(object,name) 判断object中是否有name字符串对应的属性或方法,返回Tr ...

随机推荐

  1. High-level structure of a simple compiler高級結構的簡單編譯器

    1.lexical analysis,which analyzes the character string presented to it and divides it up into tokens ...

  2. win8系统本地服务网络受限cpu占用率过高解决方案

    今天更新软件时突然就打不开软件了,接着cpu就飙升. 打开任务管理器看到是“本地服务网络受限”这么一个东西占用的cpu最高. 在网上找到的解决方案无效的: 1.关闭家庭组(服务里的homegroup· ...

  3. 添加php的memcached扩展模块

    memcached服务直接用yum安装[root@localhost ~]# yum install memcached 然后启动memcache服务,启动多个实例[root@localhost ~] ...

  4. Codeforces Round #505 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final)-C. Plasticine zebra

    问了学长,感觉还是很迷啊,不过懂了个大概,这个翻转操作,实质不就是在序列后面加上前面部分比如 bw | wwbwwbw  操作过后 wbwbwwbww 而 bw | wwbwwbwbw 这样我们就知道 ...

  5. linux内核设计第七周——可执行程序的装载

  6. 第六次作业-my Backlog

    杨灵超小组 My  Backlog                                小学生四则运算自动生成(Backlog) ID Name IMP EST How   to Demo ...

  7. 毕业设计心得与整理-APP-主题切换

    1.定义主体颜色: 在style自定义了三个属性: <item name="textLight">@android:color/white</item> & ...

  8. PAT L3-003 社交集群

    https://pintia.cn/problem-sets/994805046380707840/problems/994805053141925888 当你在社交网络平台注册时,一般总是被要求填写 ...

  9. PAT 1016 部分A+B

    https://pintia.cn/problem-sets/994805260223102976/problems/994805306310115328 正整数A的“D~A~(为1位整数)部分”定义 ...

  10. A KeyValuePair in Java

    A KeyValuePair in Java Programming & English tuble 元组 pair 对(两)元组 tuple     三元组 dozen  一打(12个).有 ...