python 字典 dict 获取值

obj={
    "n":1,
    "e":2
}
print obj.n
AttributeError: 'dict' object has no attribute 'n'
     我想用 obj.n这个获取值而不是 obj["n"]
    需要怎么修改

python字典,据我了解,应该只可以用中括号,或者get()方法获取值,如果要用 obj.n,可以使用类来实现。

class DictClass(dict):
def getattribute(self, item):
try:
super(DictClass, self).__getattribute__(item)
except:
try:
return self[item]
except:
raise BaseException('attribute error!')

def __init__(self, sourcedict):
    for k in sourcedict:
        self[k] = sourcedict[k]

if name == '__main__':
obj = {'n':1,'e':2}
result = DictClass(obj)
print result.n, result.e

 #coding:utf-8
class DictClass(dict):
  def getattribute(self, item):
    try:
      super(DictClass, self).__getattribute__(item)
    except:
      try:
        return self[item]
      except:
        raise BaseException('attribute error!')

  def __init__(self, sourcedict):
      for k in sourcedict:
        self[k] = sourcedict[k]
        exec('self.%s= sourcedict["%s"]'% (k,k))

if __name__ == '__main__':
  obj = {'n':1,'e':2}
  result = DictClass(obj)
  print result.n, result.e

本来一个由key到value的简单表示方式,非要自己改成麻烦的?