从对象获取字典 [英] Getting a dictionary from an object

查看:90
本文介绍了从对象获取字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好。


我想快速从对象创建dicts,这样就可以调用foo [''bar' ']将返回obj.bar。


以下是有效的,但如果存在一个

,我宁愿使用内置方式。有没有?


提前致谢。


class dictobj(dict):

""

class dictobj(dict):

字典d附加一个对象,

处理d [''foo' '] as d.obj.foo。

""

def __init __(self,obj):

self.obj = obj

def __getitem __(自我,密钥):

返回self.obj .__ getattribute __(密钥)


-

Thanos Tsouanas。:我的音乐: http://www.thanostsouanas.com /
http://thanos.sians.org/ 。:Sians Music: http://www.sians.org/

Hello.

I would like to have a quick way to create dicts from object, so that a
call to foo[''bar''] would return obj.bar.

The following works, but I would prefer to use a built-in way if one
exists. Is there one?

Thanks in advance.

class dictobj(dict):
"""
class dictobj(dict):
A dictionary d with an object attached to it,
which treats d[''foo''] as d.obj.foo.
"""
def __init__(self, obj):
self.obj = obj
def __getitem__(self, key):
return self.obj.__getattribute__(key)

--
Thanos Tsouanas .: My Music: http://www.thanostsouanas.com/
http://thanos.sians.org/ .: Sians Music: http://www.sians.org/

推荐答案

Thanos Tsouanasaécrit:
Thanos Tsouanas a écrit :
你好。

我想有一个快速的方法来从对象创建dicts,以便对foo [''bar'']的调用将返回obj.bar。

以下工作,但我更喜欢使用内置如果一个存在的方式。有吗?

提前致谢。

class dictobj(dict):
"""
class dictobj(dict):
附有对象的字典d,
将d [''foo'']视为d.obj.foo。
"""
def __init __(self,obj):
self.obj = obj
def __getitem __(self,key):
返回self.obj .__ getattribute __(key)
Hello.

I would like to have a quick way to create dicts from object, so that a
call to foo[''bar''] would return obj.bar.

The following works, but I would prefer to use a built-in way if one
exists. Is there one?

Thanks in advance.

class dictobj(dict):
"""
class dictobj(dict):
A dictionary d with an object attached to it,
which treats d[''foo''] as d.obj.foo.
"""
def __init__(self, obj):
self.obj = obj
def __getitem__(self, key):
return self.obj.__getattribute__(key)




我会用以下内容替换最后一行:

返回getattr(self.obj,key)

现在给出你的规格,我不知道看看你的解决方案出了什么问题。



I''d replace this last line with:
return getattr(self.obj, key)
Now given your specs, I don''t see what''s wrong with your solution.


2005年7月23日星期六上午11:30:19 + 0200,Bruno Desthuilliers写道:
On Sat, Jul 23, 2005 at 11:30:19AM +0200, Bruno Desthuilliers wrote:
Thanos Tsouanasaécrit:
Thanos Tsouanas a écrit :
class dictobj(dict):
"""
class dictobj(dict):
一本字典d附有一个物体,
对待d [''foo''] a s d.obj.foo。
"""
def __init __(self,obj):
self.obj = obj
def __getitem __(self,key):
返回self.obj .__ getattribute __(key)
class dictobj(dict):
"""
class dictobj(dict):
A dictionary d with an object attached to it,
which treats d[''foo''] as d.obj.foo.
"""
def __init__(self, obj):
self.obj = obj
def __getitem__(self, key):
return self.obj.__getattribute__(key)



我会用以下内容替换最后一行:
return getattr(self.obj,key)

现在给出你的规格,我不知道你的解决方案有什么问题。



I''d replace this last line with:
return getattr(self.obj, key)

Now given your specs, I don''t see what''s wrong with your solution.




我只是不想用我的班级,如果一个已经存在于

库中(或任何其他方式来实现相同的东西),那就是全部;)


谢谢你小费。


-

Thanos Tsouanas。:我的音乐: http://www.thanostsouanas.com/
http://thanos.sians.org/ 。:Sians Music: http ://www.sians.org/



I just dont want to use my class, if one already exists in the
libraries (or any other way to achieve the same thing), that''s all ;)

Thanks for the tip.

--
Thanos Tsouanas .: My Music: http://www.thanostsouanas.com/
http://thanos.sians.org/ .: Sians Music: http://www.sians.org/


2005年7月23日星期六11:48:27 +0300,Thanos Tsouanas写道:
On Sat, 23 Jul 2005 11:48:27 +0300, Thanos Tsouanas wrote:
你好。


这看起来让我很困惑。为什么不直接调用obj.bar,因为它/ b $ b看起来你实际上并没有使用字典?

以下是有效的,但我更喜欢如果存在,则使用内置方式。有吗?

提前致谢。

class dictobj(dict):
"""
class dictobj(dict):
附有对象的字典d,
将d [''foo'']视为d.obj.foo。
"""
def __init __(self,obj):
self.obj = obj
def __getitem __(self,key):
返回self.obj .__ getattribute __(key)
Hello.

I would like to have a quick way to create dicts from object, so that a
call to foo[''bar''] would return obj.bar.
That looks rather confusing to me. Why not just call obj.bar, since it
doesn''t look like you are actually using the dictionary at all?
The following works, but I would prefer to use a built-in way if one
exists. Is there one?

Thanks in advance.

class dictobj(dict):
"""
class dictobj(dict):
A dictionary d with an object attached to it,
which treats d[''foo''] as d.obj.foo.
"""
def __init__(self, obj):
self.obj = obj
def __getitem__(self, key):
return self.obj.__getattribute__(key)




我不认为这是特别有用的行为。你是如何使用它的?


py> D = dictobj(hello world)

py> D $ / $
{}

py> D.obj

''你好世界''

py> D [" food] =垃圾邮件

py> D $ / $
{''food'':''垃圾邮件''}

py> D [" food]

Traceback(最近一次调用最后一次):

文件"< stdin>",第1行,在?

文件"< stdin>",第5行,在__getitem__

AttributeError:''str''对象没有属性''food''

-

史蒂文。



I don''t think this is particularly useful behaviour. How do you use it?

py> D = dictobj("hello world")
py> D
{}
py> D.obj
''hello world''
py> D["food"] = "spam"
py> D
{''food'': ''spam''}
py> D["food"]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 5, in __getitem__
AttributeError: ''str'' object has no attribute ''food''
--
Steven.


这篇关于从对象获取字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆