字典和点符号 [英] Dictionaries and dot notation
问题描述
对于大多数人来说这可能是非常明显的:
当我有一个对象(一个类的实例Foo)时,我可以访问
属性通过点表示法:
aFoo.bar
但是当我有一本字典时
aDict = {" bar":" something"}
我必须写
aDict [" bar"]
如果我想创建一个可用于点
表示法而不必创建类的数据结构,即因为那些对象
根本没有任何行为?
我知道通过括号表示法访问实例变量
真的必须写成:
aFoo .__ dict __ [''bar'']
但这并没有让我更进一步,因为我仍然需要
将__dict__内容插入到我的数据结构中,这导致我们与上面的问题相同。
谁能告诉我这里缺少什么?
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写道:
Martin Drautzburg wrote:
对于大多数人来说这可能是非常明显的:
当我有一个对象(一个类的实例Foo)时,我可以访问
属性通过点表示法:
aFoo.bar
但是当我有一本字典时
aDict = {" bar":" something"}
我必须写
aDict [" ; bar"]
如果我想创建一个可以在点
表示法中使用而不必创建类的数据结构,即因为那些对象,该怎么办?
根本没有任何行为?
我知道通过括号表示法访问实例变量
真的必须写as:
aFoo .__ dict __ [''bar'']
b这不会给我带来任何进一步的影响,因为我仍然需要将__dict__的东西插入我的数据结构中,这导致我们与上面的问题相同。
谁能告诉我这里缺少什么?
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?
这个?
http://docs.python.org/ref/attribute-access.html
Stefan
This?
http://docs.python.org/ref/attribute-access.html
Stefan
对于你们大多数人来说这可能是非常明显的:
This may be pretty obvious for most of you:
当我有一个对象(一个类的实例Foo)我可以通过点表示法访问
属性:
aFoo.bar
但是当我有一本字典时
aDict = {" bar":some something}
我必须写
aDict [" bar"]
如果我想创建一个可用于dot的数据结构怎么办? />
表示法,无需创建类,即因为那些对象
根本没有行为?
我知道访问instanc e变量通过括号表示
真的必须写成:
aFoo .__ dict __ [''bar'']
>
但这并没有给我带来任何进一步的影响,因为我仍然需要将__dict__的东西插入我的数据结构中,这导致我们
同样的问题。
谁能告诉我这里缺少什么?
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?
创建虚拟课有什么问题?
班级数据:
通票
mydata = data()
mydata.foo =''foo''
mydata.bar ='''''
打印mydata.foo
打印mydata.bar
Daniel
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 Drautzburgaécrit:
Martin Drautzburg a écrit :
对于大多数人来说这可能是非常明显的:
>
当我有一个对象(一个类的实例Foo)时,我可以通过点表示法访问
属性:
aFoo .bar
但是当我有一本字典时
aDict = {" bar":某事}
我必须写
aDict [" bar"]
如果我想创建数据结构怎么办?这可以用在点
表示法中,而不必创建一个cl屁股,即因为那些物品
根本没有行为?
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?
从dict继承并实现__getattr__和
__setattr__的类应该可以解决这个问题...
A class inheriting from dict and implementing __getattr__ and
__setattr__ should do the trick...
这篇关于字典和点符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!