class Foo:
def __getitem__(self, item):
print('=====>get')
return self.__dict__[item] def __setitem__(self, key, value):
self.__dict__[key] = value
# setattr(self,key,value) def __delitem__(self, key):
self.__dict__.pop(key) f = Foo()
f.x = 1
print(f.x)
print(f.__dict__)

1、增加

f['x'] = 123123123123
print(f.__dict__)
f['x']

2、删除 

del f['x']
print(f['x'])

  

day7_python之面向对象item系列(__getitem__,__setitem__,__delitem__)的更多相关文章

  1. python 魔法方法之:__getitem__ __setitem__ __delitem__

    h2 { color: #fff; background-color: #7CCD7C; padding: 3px; margin: 10px 0px } h3 { color: #fff; back ...

  2. python中的__len__,__getitem__ __setitem__ __delitem__ __contains__

    可变集合需要实现: __len__  __getitem__    __setitem__  __delitem__不可变集合需要实现: __len__  __getitem__ __len__:返回 ...

  3. __getitem__ __setitem__ __delitem__ 使用

    #__getitem__ __setitem__ __delitem__运行设置key value值了class fun: def __init__(self): print('test') def ...

  4. __getitem__,__setitem__,__delitem__

    __getitem__.__setitem__.__delitem__ 总结: __getitem__,__setitem_,__delitem__ : obj[‘属性’]的方式去操作属性时触发的方法 ...

  5. Python的魔法函数之 - __len__,__getitem__,__setitem__,__delitem__

    # 对象作为len()函数的参数是必须实现该方法 __len__ # 使用类似字典方式访问成员时必须实现 dic['pro_name'] __getitem__ # 使用类似字典方式设置成员时必须实现 ...

  6. 面向对象 反射 和item系列和内置函数和__getattr__和__setattr__

    反射 反射主要用在网络编程中, python面向对象的反射:通过字符串的形式操作对象相关的属性.python的一切事物都是对象. 反射就是通过字符串的形式,导入模块:通过字符串的形式,去模块寻找指定函 ...

  7. python面向对象( item系列,__enter__ 和__exit__,__call__方法,元类)

    python面向对象进阶(下)   item系列 __slots__方法 __next__ 和 __iter__实现迭代器  析构函数 上下文管理协议 元类一.item系列 把对象操作属性模拟成字典的 ...

  8. 面向对象的进阶(item系列,__new__,__hash__,__eq__)

      面向对象的进阶(item系列,__new__,__hash__,__eq__) 一.item系列 getitem.setitem.delitem(操作过程达到的结果其实就是增删改查) class ...

  9. 面向对象进阶-item系列、__new__、__hash__、__eq__ (四)

    item系列 dic = {'k':'v'}# 对象 : 存储属性 和调用方法dic['k'] = 'v'# class Foo:#     def __init__(self,name,age,se ...

随机推荐

  1. Breakpoint 断点只生效一次

  2. css不定高图文垂直居中的三种方法

    html部分 <div class="box"> <img class="img" src="http://p2.so.qhmsg. ...

  3. AspNet 常有功能函数1.0

    1.net 获取客户端ip方法(此方法不是很准确) public static string GetIP() { string str; if (!string.IsNullOrEmpty(HttpC ...

  4. Poj 2104区间第k大(归并树)

    题目链接 K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 36890 Accepted: 11860 C ...

  5. php 单向散列加密

    1.加密文件 <?php //sha1_en.php header("content-type:text/html;charset=utf-8"); $str = " ...

  6. 学习JDK1.8集合源码之--Vector

    1. Vector简介 Vector是JDK1.0版本就推出的一个类,和ArrayList一样,继承自AbstractList,实现了List.RandomAccess.Cloneable.java. ...

  7. 【水滴石穿】douban-movies-react-native

    这个项目的话,倒是可以做一个支架页面,就是你需要什么东西,你就可以在里面加,不过也是比较难的地方 就是数据流,数据处理的部分.react可以处理数据的方式很多,没有见过类似于古老的vue时候可以使用的 ...

  8. JavaScript--放大镜

    上例图: <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF- ...

  9. PHP学习(类和对象)——基本概念

    类是面向对象程序设计的基本概念,通俗的理解类就是对现实中某一个种类的东西的抽象, 比如汽车可以抽象为一个类,汽车拥有名字.轮胎.速度.重量等属性,可以有换挡.前进.后退等操作方法. 每个类的定义都以关 ...

  10. 如何用Excel打开CSV文件

    如何用Excel打开CSV文件? CSV文件一般是MS-SQL 导出查询数据的一种格式.格式结构是 用逗号分隔数据,如果直接用Excel打开那么数据不会自动分列.需要进行一定的设置.下面是设置过程. ...