day7_python之面向对象item系列(__getitem__,__setitem__,__delitem__)
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__)的更多相关文章
- python 魔法方法之:__getitem__ __setitem__ __delitem__
h2 { color: #fff; background-color: #7CCD7C; padding: 3px; margin: 10px 0px } h3 { color: #fff; back ...
- python中的__len__,__getitem__ __setitem__ __delitem__ __contains__
可变集合需要实现: __len__ __getitem__ __setitem__ __delitem__不可变集合需要实现: __len__ __getitem__ __len__:返回 ...
- __getitem__ __setitem__ __delitem__ 使用
#__getitem__ __setitem__ __delitem__运行设置key value值了class fun: def __init__(self): print('test') def ...
- __getitem__,__setitem__,__delitem__
__getitem__.__setitem__.__delitem__ 总结: __getitem__,__setitem_,__delitem__ : obj[‘属性’]的方式去操作属性时触发的方法 ...
- Python的魔法函数之 - __len__,__getitem__,__setitem__,__delitem__
# 对象作为len()函数的参数是必须实现该方法 __len__ # 使用类似字典方式访问成员时必须实现 dic['pro_name'] __getitem__ # 使用类似字典方式设置成员时必须实现 ...
- 面向对象 反射 和item系列和内置函数和__getattr__和__setattr__
反射 反射主要用在网络编程中, python面向对象的反射:通过字符串的形式操作对象相关的属性.python的一切事物都是对象. 反射就是通过字符串的形式,导入模块:通过字符串的形式,去模块寻找指定函 ...
- python面向对象( item系列,__enter__ 和__exit__,__call__方法,元类)
python面向对象进阶(下) item系列 __slots__方法 __next__ 和 __iter__实现迭代器 析构函数 上下文管理协议 元类一.item系列 把对象操作属性模拟成字典的 ...
- 面向对象的进阶(item系列,__new__,__hash__,__eq__)
面向对象的进阶(item系列,__new__,__hash__,__eq__) 一.item系列 getitem.setitem.delitem(操作过程达到的结果其实就是增删改查) class ...
- 面向对象进阶-item系列、__new__、__hash__、__eq__ (四)
item系列 dic = {'k':'v'}# 对象 : 存储属性 和调用方法dic['k'] = 'v'# class Foo:# def __init__(self,name,age,se ...
随机推荐
- Linux部署教育云平台测试环境总结
2016年10月16日换了新公司,刚进公司就要进行平台测试环境部署,由于之前Linux用的比较少,只用过几个简单的杀进程.重启tomcat.查看日志等简单的操作命令,真要在LInux服务器上部署环境的 ...
- 【react】react-reading-track
这是一个很有趣的图书阅读demo 先放github地址:https://github.com/onlyhom/react-reading-track 我觉得这个博主的项目很有意思呢 我们一起看看代码啊 ...
- string型的“600.000”如何转换为int型
string型的“600.000”怎么转换为int型?为什么我用int.parse不能转换? ------解决方案--------------------int.Parse("600.000 ...
- 安装 cx_Oracle
1.下载 oracle client instant 和 sdk, 全部解压到 /opt/instantclient_11_2/ http://www.oracle.com/technetwor ...
- php 简单加密解密
<?php namespace App\Service; /* * @link http://kodcloud.com/ * @author warlee | e-mail:kodcloud@q ...
- DateTimeFormatter
//解析日期 String dateStr= "2018年12月18日"; DateTimeFormatter formatter = DateTimeFormatter.ofPa ...
- SDUT-3378_数据结构实验之查找六:顺序查找
数据结构实验之查找六:顺序查找 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 在一个给定的无序序列里,查找与给定关键字 ...
- 【模板】树的重心 洛谷P1364 医院设置
P1364 医院设置 题目描述 设有一棵二叉树,如图: 其中,圈中的数字表示结点中居民的人口.圈边上数字表示结点编号,现在要求在某个结点上建立一个医院,使所有居民所走的路程之和为最小,同时约定,相邻接 ...
- #define 和常量 const 的区别
const 后的常量,程序对其中只能读不能修改. #include <iostream> using namespace std; int main() { const double pi ...
- postman测试接口各种类型传值
postman测试接口各种类型传值 标签: postman测试 json串 Map 2018年01月27日 02:32:00 145人阅读 评论(0) 收藏 举报 1.Map类型或实体类类型传值,即j ...