python class 使用
#!/usr/bin/env python #coding=utf-8 #设置python编码 from operator import itemgetter import sys ###### class MyMap: def __init__(self): self.entity={} print('Initialize {0}'.format('MyMap')) def __del__(self): print('析构函数MyMap size: {0:d}'.format(len(self.entity))) #del self.entity del self def hasConstantKey(self,key): return self.entity.has_key(key) def put(self,key,val): self.entity[key]=val def get(self,key): if(self.hasConstantKey(key)): return self.entity[key] else: return None def toString(self): return self.entity def keys(self): return self.entity.keys() def values(self): return self.entity.values() def remove(self,key): del self.entity[key]; def size(self): return len(self.entity) @staticmethod def staticMethodTest(): print '我来自静态方法' @staticmethod def staticTest(val): if(val): print 'val:',True else: print 'val:',False print "-------------使用class的方式演示了系统自带字典使用-----------------" myMap=MyMap() print myMap.hasConstantKey('abc') myMap.put('abc','sasdaasdas') print myMap.hasConstantKey('abc') print myMap.get('abc') print myMap.toString() print myMap.keys() print myMap.values() print myMap.size() print MyMap.staticMethodTest() MyMap.staticTest(myMap)
运行Python
$ python test_class.py
-------------使用class的方式演示了系统自带字典使用-----------------
Initialize MyMap
False
True
sasdaasdas
{'abc': 'sasdaasdas'}
['abc']
['sasdaasdas']
1
我来自静态方法
None
val: True
析构函数MyMap size: 1