__getitem__()、__setitem__()与__delitem__()
# 如果想要运用[]取值,可以实现__getitem__()
# 想要运用[]设值,可以实现__setitem__()
# 若想通过del与[]来删除,可以实现__delitem__()
class ChainMap:
def __init__(self,*maps):
self.maps=maps
def lookup(self,key): # 查找是否有对应键的字典
for m in self.maps:
if key in m:
return m
return None
def __getitem__(self, item): # 实现__getitem__()方法
m=self.lookup(item)
if m:
return m[item]
else:
raise KeyError
def __setitem__(self, key, value): # 实现__setitem__()方法
m=self.lookup(key)
if m:
m[key]=value
else:
self.maps[key]=value
def __delitem__(self, key): # 实现__delitem__()方法
m=self.lookup(key)
if m:
del m[key]
else:
raise KeyError
c=ChainMap({'A':'Justin'},{'A':'Monica','B':'Irene'})
print(c.maps)
print(c['A'])
c['A']='caterpillar'
print(c.maps)
del c['A']
print(c.maps)
__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 ...
- 9.4、__del__、__doc__、__dict__、__module__、__getitem__、__setitem__、__delitem__、__str__、__repr__、__call__
相关内容: __del__.__doc__.__dict__.__module__.__getitem__.__setitem__.__delitem__.__str__.__repr__.__cal ...
- __getitem__,__setitem__,__delitem__
__getitem__.__setitem__.__delitem__ 总结: __getitem__,__setitem_,__delitem__ : obj[‘属性’]的方式去操作属性时触发的方法 ...
- __setitem__,__getitem,__delitem__
目录 __setitem__ __getitem__ __delitem__与__delattr__ class Foo: def __init__(self, name): self.name = ...
- Python的魔法函数之 - __len__,__getitem__,__setitem__,__delitem__
# 对象作为len()函数的参数是必须实现该方法 __len__ # 使用类似字典方式访问成员时必须实现 dic['pro_name'] __getitem__ # 使用类似字典方式设置成员时必须实现 ...
- 面向对象:__getitem__、__setitem__、__delitem__
item系列 class Person(object): def __init__(self, name): self.name = name def __getitem__(self, item): ...
- day7_python之面向对象item系列(__getitem__,__setitem__,__delitem__)
class Foo: def __getitem__(self, item): print('=====>get') return self.__dict__[item] def __setit ...
随机推荐
- Codeforces Beta Round #79 (Div. 2 Only)
Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...
- 微信小程序开发——活动规则类文案文件读取及自动转换为小程序排版代码
前言: 最近做的小程序活动规则内容比较多,且一直处于修改中.由于小程序并不支持类似Html5中的预排版,所以,活动规则内容修改较大的时候,仍需要对新的内容用小程序的<text>组件做下排版 ...
- [剑指Offer]34-二叉树中和为某一值的路径
题目链接 https://www.nowcoder.com/practice/b736e784e3e34731af99065031301bca?tpId=13&tqId=11177&t ...
- 函数名、闭包、装饰器 day11
1, 函数名的内存地址,print(func) 2, 函数名可以赋值给其他变量 3, 函数名可以当做容器类的元素 4, 函数名可以当做函数的参数. 5, 函数名可以当做函数的返回值. 学名:第一对象 ...
- HTTP Basic Authentication认证(Web API)
当下最流行的Web Api 接口认证方式 HTTP Basic Authentication: http://smalltalllong.iteye.com/blog/912046 什么是HTTP B ...
- Android开发之炫酷MD风格
文章转自:一点点征服的 http://www.cnblogs.com/ldq2016/p/5217590.html 安卓开发中非常炫的效果集合 这几天开发的时候,想做一些好看而且酷炫的特效,于是又开始 ...
- iOS.Objective-C.Dependency.Graphing-v0.1
当Project越来越复杂,模块间的依赖就会很复杂,不合理的依赖就出现:不必要的依赖,双向依赖等等. 在iOS Application Project中可以将依赖定义为:对某个头文件的import. ...
- Porsche PIWIS III with V37.250.020 Piwis 3 Software Update New Feature
Porsche Piwis tester 3 PT3G VCI with V37.250.020 Piwis 3 Software unlimited license installed on Ful ...
- 在eclipse中import java web项目时遇到的一些问题并将该项目通过tomcat发布
1.首先是import一个新的项目,会将已有的项目import到working space中,注意,你现在的项目路径就在working space了,而不是已有的项目路径! 2.点击eclipse上面 ...
- 立即响应ScrollView上的子视图的手势
self.myScrollView.delaysContentTouches = YES; self.myScrollView.CanCancelContentTouches=NO; 写了一个继承sc ...