本文是小编为大家收集整理的关于字典和点符号的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。
问题描述
这对你们大多数人来说可能很明显:
当我有一个对象(类"Foo"的一个实例)时,我可以访问
点符号的属性:
aFoo.bar
但是当我有字典时
aDict = {"bar":"something"}
我要写
aDict["bar"]
如果我想创建一个可以在 dot
中使用的数据结构怎么办?符号而不必创建一个类,即因为那些对象
一点行为都没有?
我知道通过括号表示法访问实例变量会
真的要写成:
aFoo.__dict__[''bar'']
但这并没有让我更进一步,因为我仍然必须
将那个 __dict__ 插入到我的数据结构中,这导致我们
和上面一样的问题.
谁能告诉我这里缺少什么?
推荐答案
Martin Drautzburg 写道:这对你们大多数人来说可能很明显:这个?
当我有一个对象(类"Foo"的一个实例)时,我可以访问
通过点表示法的属性:
aFoo.bar
但是当我有字典时
aDict = {"bar":"something"}
我要写
aDict["条形"]
如果我想创建一个可以在 dot
中使用的数据结构怎么办?符号而不必创建一个类,即因为那些对象
一点行为都没有?
我知道通过括号表示法访问实例变量会
真的要写成:
aFoo.__dict__[''bar'']
但这并没有让我更进一步,因为我仍然必须
将那个 __dict__ 插入到我的数据结构中,这导致我们
和上面一样的问题.
谁能告诉我我在这里缺少什么?
http://docs.python.org/ref/attribute-access.html
Stefan
这对你们大多数人来说可能很明显:
当我有一个对象(类"Foo"的一个实例)时,我可以访问
点符号的属性:
aFoo.bar
但是当我有字典时
aDict = {"bar":"something"}
我要写
aDict["bar"]
如果我想创建一个可以在 dot
中使用的数据结构怎么办?符号而不必创建一个类,即因为那些对象
一点行为都没有?
我知道通过括号表示法访问实例变量会
真的要写成:
aFoo.__dict__[''bar'']
但这并没有让我更进一步,因为我仍然必须
将那个 __dict__ 插入到我的数据结构中,这导致我们
和上面一样的问题.
谁能告诉我我在这里缺少什么?
创建一个虚拟类有什么问题?
班级资料:
通过
我的数据=数据()
mydata.foo = ''foo''
mydata.bar = ''bar''
打印 mydata.foo
打印 mydata.bar
丹尼尔
Martin Drautzburg 一个 écrit :这对你们大多数人来说可能很明显:继承自dict并实现__getattr__和
当我有一个对象(类"Foo"的一个实例)时,我可以访问
通过点表示法的属性:
aFoo.bar
但是当我有字典时
aDict = {"bar":"something"}
我要写
aDict["条形"]
如果我想创建一个可以在 dot
中使用的数据结构怎么办?符号而不必创建一个类,即因为那些对象
完全没有行为?
的类__setattr__ 应该可以解决问题...
问题描述
This may be pretty obvious for most of you:
When I have an object (an instance of a class "Foo") I can access
attributes via dot notation:
aFoo.bar
however when I have a dictionary
aDict = {"bar":"something"}
I have to write
aDict["bar"]
What if I want to create a datastructure that can be used in dot
notation without having to create a class, i.e. because those objects
have no behavior at all?
I know that accessing an instance variable via bracket notation would
really have to be written as:
aFoo.__dict__[''bar'']
but this does not bring me any further, because I would still have to
plug in that __dict__ thing into my datastructure, which leads us to
the same question as above.
Can anyone tell me what I am missing here?
推荐答案
Martin Drautzburg wrote:This may be pretty obvious for most of you:This?
When I have an object (an instance of a class "Foo") I can access
attributes via dot notation:
aFoo.bar
however when I have a dictionary
aDict = {"bar":"something"}
I have to write
aDict["bar"]
What if I want to create a datastructure that can be used in dot
notation without having to create a class, i.e. because those objects
have no behavior at all?
I know that accessing an instance variable via bracket notation would
really have to be written as:
aFoo.__dict__[''bar'']
but this does not bring me any further, because I would still have to
plug in that __dict__ thing into my datastructure, which leads us to
the same question as above.
Can anyone tell me what I am missing here?
http://docs.python.org/ref/attribute-access.html
Stefan
This may be pretty obvious for most of you:
When I have an object (an instance of a class "Foo") I can access
attributes via dot notation:
aFoo.bar
however when I have a dictionary
aDict = {"bar":"something"}
I have to write
aDict["bar"]
What if I want to create a datastructure that can be used in dot
notation without having to create a class, i.e. because those objects
have no behavior at all?
I know that accessing an instance variable via bracket notation would
really have to be written as:
aFoo.__dict__[''bar'']
but this does not bring me any further, because I would still have to
plug in that __dict__ thing into my datastructure, which leads us to
the same question as above.
Can anyone tell me what I am missing here?
What''s wrong with creating a dummy class?
class data:
pass
mydata = data( )
mydata.foo = ''foo''
mydata.bar = ''bar''
print mydata.foo
print mydata.bar
Daniel
Martin Drautzburg a écrit :This may be pretty obvious for most of you:A class inheriting from dict and implementing __getattr__ and
When I have an object (an instance of a class "Foo") I can access
attributes via dot notation:
aFoo.bar
however when I have a dictionary
aDict = {"bar":"something"}
I have to write
aDict["bar"]
What if I want to create a datastructure that can be used in dot
notation without having to create a class, i.e. because those objects
have no behavior at all?
__setattr__ should do the trick...