在Ruby中,有一个名为Mash的真棒库,它是一个哈希,但通过巧妙地使用missing_method可以转换:
object['property']
到
object.property
这对模拟非常有用。有人知道Python中有类似的东西吗?
答案 0 :(得分:6)
你是否绝对有必要以dict为基础? Python对象可以通过非常少的额外管道动态获取属性:
>>> class C(object): pass
...
>>> z = C()
>>> z.blah = "xyzzy"
>>> dir(z)
['__class__', '__delattr__', '__dict__', ... '__weakref__', 'blah']
答案 1 :(得分:3)
__getitem__
您要找的是什么?
class C:
def __init__(self):
self.my_property = "Hello"
def __getitem__(self, name):
return getattr(self, name)
c = C()
print c['my_property'] # Prints "Hello"
或者您正在寻找相反的方式,通过__getattr__
?
class D(dict):
def __getattr__(self, name):
return self[name]
d = D()
d['x'] = "Hello"
print d.x # Prints "Hello"
(编辑:正如Paul McGuire在评论中指出的那样,这段代码只展示了完整解决方案的基本原理。)
答案 2 :(得分:1)
你是否绝对有必要以dict为基础?
是,如果您希望将其视为项目列表,而不会滥用__dict__
。
以下是我对Mash问题的旧回答。它提供了一个默认值,默认值可以是方法或对象,如果它是一个对象,它将被深深克隆(不只是热链接),如果它被多次使用。
它将其简单的键值公开为.key
:
def Map(*args, **kwargs):
value = kwargs.get('_default', None)
if kwargs.has_key('_default'): del kwargs['_default']
# CONSIDER You may want to look at the collections.defaultdict class.
# It takes in a factory function for default values.
#
# You can also implement your class by overriding the __missing__ method
# of the dict class, rather than overriding the __getitem__.
#
# Both were added in Python 2.5 according to the documentation.
class _DefMap(dict):
'But CONSIDER http://pypi.python.org/pypi/bunch/1.0.0 '
def __init__(self, *a, **kw):
dict.__init__(self, *a, **kw)
self.__dict__ = self
def __getitem__(self, key):
if not self.has_key(key):
if hasattr(value, '__call__'):
self[key] = value(key)
else:
self[key] = copy.deepcopy(value)
return dict.__getitem__(self, key)
return _DefMap(*args, **kwargs)