班级attrdict [英] class attrdict

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

问题描述

这个课程还需要更多吗?

是否存在查找循环的风险?

似乎工作......


class attrdict(dict):

""" Dict其中d [''foo'']也可以作为d.foo""
访问
def __init __(self,* args,** kwargs):

self .__ dict__ = self

dict .__ init __(self,* args,** kwargs )

def __repr __(自我):

返回dict .__ repr __(self).join((" attrdict(",")"))

Does this class need anything more?
Is there any risk of a lookup loop?
Seems to work...

class attrdict(dict):
"""Dict where d[''foo''] also can be accessed as d.foo"""
def __init__(self, *args, **kwargs):
self.__dict__ = self
dict.__init__(self, *args, **kwargs)
def __repr__(self):
return dict.__repr__(self).join(("attrdict(", ")"))


>> a = attrdict([(1,2)],a = 3, b = 4)
a
>>a = attrdict([(1,2)], a=3, b=4)
a



attrdict({'''':3,1:2,''' b'':4})

attrdict({''a'': 3, 1: 2, ''b'': 4})


>> a = attrdict([(1 ,2)],b = 3,c = 4)
a
>>a = attrdict([(1,2)], b=3, c=4)
a



attr dict({1:2,''c'':4,''b'':3})

attrdict({1: 2, ''c'': 4, ''b'': 3})


>> ab
>>a.b



3

3


>> ad = 5
a [''d'']
>>a.d = 5
a[''d'']



5

5


>> ae
>>a.e



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

文件"< stdin>",line 1,in?

AttributeError:''attrdict''对象没有属性''e''

Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: ''attrdict'' object has no attribute ''e''


>> a .__ getattr__ ='''xyzzy''
a .__ getattribute__ ='' xyzzy''
a .__ setattr__ =''xyzzy''
a .__ delattr__ ='''xyzzy''
ac
>>a.__getattr__ = ''xyzzy''
a.__getattribute__ = ''xyzzy''
a.__setattr__ = ''xyzzy''
a.__delattr__ = ''xyzzy''
a.c



4

4


>> a [1]
>>a[1]



2

2


>> del ac
>>del a.c



-

Hallvard

推荐答案

Hallvard B Furuseth写道:
Hallvard B Furuseth wrote:

这门课还需要更多吗?

是否存在查找循环的风险?

似乎工作...


class attrdict(dict):

""" Dict其中d [''foo'']也可以作为d.foo访问""

def __init __(self,* args,**夸rgs):

self .__ dict__ = self

dict .__ init __(self,* args,** kwargs)

def __repr __(self) :

返回dict .__ repr __(self).join((" attrdict(",")"))
Does this class need anything more?
Is there any risk of a lookup loop?
Seems to work...

class attrdict(dict):
"""Dict where d[''foo''] also can be accessed as d.foo"""
def __init__(self, *args, **kwargs):
self.__dict__ = self
dict.__init__(self, *args, **kwargs)
def __repr__(self):
return dict.__repr__(self).join(("attrdict(", ")"))



奇怪的是,这似乎没问题,因为dict子类的实例

对象无论如何都有一个空的__dict__属性,因此你不会无意中破坏某些行为。

James

Strangely enough, this seems okay since an instance of a dict subclass
object has an empty __dict__ attribute anyway and so you won''t be
unwittingly destroying some behavior.

James


Hallvard B Furuseth< h。********** @ usit.uio.nowrote :
Hallvard B Furuseth <h.**********@usit.uio.nowrote:

这个类还需要更多吗?

是否存在查找循环的风险?

似乎工作......


class attrdict(dict):

""" Dict d [''foo'']也可以作为d.foo访问""

def __init __(self,* args,** kwargs):

self .__ dict__ = self

dict .__ init __(self,* args,** kwargs)

def __repr __(self):

return dict .__ repr __(self).join((" attrdict(") ;,")"))
Does this class need anything more?
Is there any risk of a lookup loop?
Seems to work...

class attrdict(dict):
"""Dict where d[''foo''] also can be accessed as d.foo"""
def __init__(self, *args, **kwargs):
self.__dict__ = self
dict.__init__(self, *args, **kwargs)
def __repr__(self):
return dict.__repr__(self).join(("attrdict(", ")"))



问题主要在于,给定一个attrdict实例,你是否可以调用
(例如)a.update(foo)取决于你是否设置了

a [''update''],使整个程序非常脆弱 - 非常高

支付一些适量语法糖的价格。

Alex

The problem is mostly that, given an instance a of attrdict, whether you
can call (e.g.) a.update(foo) depends on whether you ever set
a[''update''], making the whole program extremely fragile -- a very high
price to pay for some modest amount of syntax sugar.
Alex


3月2日晚上9:25,a ... @ mac .com(Alex Martelli)写道:
On Mar 2, 9:25 pm, a...@mac.com (Alex Martelli) wrote:

问题主要在于,给定一个attrdict实例,你是否可以调用(b $ b)例如a.update(foo)取决于你是否设置了

a [''update''],使整个程序非常脆弱 - 一个非常高的价值b $ b价格支付一些适量的语法糖。
The problem is mostly that, given an instance a of attrdict, whether you
can call (e.g.) a.update(foo) depends on whether you ever set
a[''update''], making the whole program extremely fragile -- a very high
price to pay for some modest amount of syntax sugar.



怎么样......


class attrdict(dict):

def __init __(self,* args,** kwargs):

dict .__ init __(self,* args,** kwargs)

for k,v in self.items( ):

dict .__ setattr __(self,str(k),v)

def __setitem __(self,k,v):

dict .__ setitem __(self,k,v)

dict .__ setattr __(self,str(k),v)

__setattr__ = __setitem__


问候,

Jordan

How about something like...

class attrdict(dict):
def __init__(self, *args, **kwargs):
dict.__init__(self, *args, **kwargs)
for k, v in self.items():
dict.__setattr__(self, str(k), v)
def __setitem__(self, k, v):
dict.__setitem__(self, k, v)
dict.__setattr__(self, str(k), v)
__setattr__ = __setitem__

Regards,
Jordan


这篇关于班级attrdict的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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