如何使用dict对象的__setitem__方法? [英] How can I use __setitem__ method of dict object?

查看:157
本文介绍了如何使用dict对象的__setitem__方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请原谅我,如果这对其他人来说是显而易见的,但我无法理解

out。我是dict的子类,但是想要防止直接更改

一些键/值对。为此,我认为我应该覆盖

__setitem__方法:

class xs(dict):

"""

XS是一个容器对象,用于保存有关横截面的信息。

"""


def __new __(cls, xS = 1.0,xF = 1.0,xG = 1.0,nu = 1.0,debug = 0):

"""

"""

x = {}

x [''xS''] = xS

x [''xF''] = xF

x [''nu''] = nu

x [''xG''] = xG

x [''xA''] = x [''xG' '] + x [''xF'']

x [''xT''] = x [''xA''] + x [''xS'']


返回x


def __setitem __(自我,键,值):

"""

我已经覆盖了这种方法,以防止在

类之外设置xT或xA



"""

print"我在__setitem __"

if key =='' xT'':

引发AttributeError(""""'不能更改xT。请更改,

xF,xS或xG""" )

但是我甚至无法运行__setitem__。示例:

Python 2.5(r25:51918,2006年9月19日,08:49:13)

[GCC 4.0.1(Apple Computer,Inc。build 5341)]在达尔文上

输入help,copyright,credit等等。或许可证或欲获得更多信息。

Please excuse me if this is obvious to others, but I can''t figure it
out. I am subclassing dict, but want to prevent direct changing of
some key/value pairs. For this I thought I should override the
__setitem__ method as such:
class xs(dict):
"""
XS is a container object to hold information about cross sections.
"""

def __new__(cls, xS=1.0, xF=1.0, xG=1.0, nu=1.0, debug=0):
"""
"""
x = {}
x[''xS''] = xS
x[''xF''] = xF
x[''nu''] = nu
x[''xG''] = xG
x[''xA''] = x[''xG''] + x[''xF'']
x[''xT''] = x[''xA''] + x[''xS'']

return x

def __setitem__(self, key, value):
"""
I have overridden this method to prevent setting xT or xA
outside the
class.
"""
print "I am in __setitem__"
if key == ''xT'':
raise AttributeError("""Can''t change xT. Please change,
xF, xS, or xG""")
But I can''t even get __setitem__ to run. Example:
Python 2.5 (r25:51918, Sep 19 2006, 08:49:13)
[GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwin
Type "help", "copyright", "credits" or "license" for more information.


>> import xs
cs = xs.xs()
cs
>>import xs
cs = xs.xs()
cs



{''xA'':2.0,''xF'':1.0,''xG'' :1.0,''xS'':1.0,''nu'':1.0,''xT'':3.0}

{''xA'': 2.0, ''xF'': 1.0, ''xG'': 1.0, ''xS'': 1.0, ''nu'': 1.0, ''xT'': 3.0}


>> cs [''xT''] = 3.1415
cs
>>cs[''xT''] = 3.1415
cs



{''xA'':2.0,''xF'':1.0,''xG'':1.0,''xS'':1.0,''nu'':1.0,''xT'':

3.1415000000000002}

这是__setitem__方法的用途吗?如果没有,我怎么办呢?我想做什么?

提前谢谢,

Jeremy

{''xA'': 2.0, ''xF'': 1.0, ''xG'': 1.0, ''xS'': 1.0, ''nu'': 1.0, ''xT'':
3.1415000000000002}
Is this what the __setitem__ method is for? If not, how can I do what
I want to do?
Thanks in advance,
Jeremy

推荐答案

jeremito写道:
jeremito wrote:

如果这对其他人来说很明显,请原谅我,但我可以'算不上

了。我是dict的子类,但是想要防止直接更改

一些键/值对。为此,我认为我应该覆盖

__setitem__方法:


class xs(dict):

"" ;"

XS是一个容器对象,用于保存有关横截面的信息。

"""


def __new __(cls,xS = 1.0,xF = 1.0,xG = 1.0,nu = 1.0,debug = 0):

"""

"" ;"

x = {}

x [''xS''] = xS

x [''xF''] = xF

x [''nu''] = nu

x [''xG''] = xG

x [''xA''] = x [ ''xG''] + x [''xF'']

x [''xT''] = x [''xA''] + x [''xS'']


返回x


def __setitem __(自我,键,值):

"""

我已经覆盖了这个方法,以防止在
之外设置xT或xA


class。

"""

print"我在__setitem __"

if key =='' xT'':

引发AttributeError("""'不能更改xT。请更改,

xF,xS或xG""")


但是我甚至无法运行__setitem__。示例:

Python 2.5(r25:51918,2006年9月19日,08:49:13)

[GCC 4.0.1(Apple Computer,Inc。build 5341)]在达尔文上

输入help,copyright,credit等等。或许可证或欲获得更多信息。
Please excuse me if this is obvious to others, but I can''t figure it
out. I am subclassing dict, but want to prevent direct changing of
some key/value pairs. For this I thought I should override the
__setitem__ method as such:
class xs(dict):
"""
XS is a container object to hold information about cross sections.
"""

def __new__(cls, xS=1.0, xF=1.0, xG=1.0, nu=1.0, debug=0):
"""
"""
x = {}
x[''xS''] = xS
x[''xF''] = xF
x[''nu''] = nu
x[''xG''] = xG
x[''xA''] = x[''xG''] + x[''xF'']
x[''xT''] = x[''xA''] + x[''xS'']

return x

def __setitem__(self, key, value):
"""
I have overridden this method to prevent setting xT or xA
outside the
class.
"""
print "I am in __setitem__"
if key == ''xT'':
raise AttributeError("""Can''t change xT. Please change,
xF, xS, or xG""")
But I can''t even get __setitem__ to run. Example:
Python 2.5 (r25:51918, Sep 19 2006, 08:49:13)
[GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

>>> import xs
cs = xs.xs()
cs
>>>import xs
cs = xs.xs()
cs



{''xA'':2.0,''xF'':1.0,''xG'':1.0,''xS'':1.0 ,''nu'':1.0,''xT'':3.0}

{''xA'': 2.0, ''xF'': 1.0, ''xG'': 1.0, ''xS'': 1.0, ''nu'': 1.0, ''xT'': 3.0}


>>> cs [' 'xT''] = 3.1415
cs
>>>cs[''xT''] = 3.1415
cs



{''xA'':2.0,''xF'':1.0,' 'xG'':1.0,''xS'':1.0,''nu'':1.0,''xT'':

3.1415000000000002}


这是__setitem__方法的用途吗?如果没有,我该怎么办?我想做什么?b $ b我想做什么?

{''xA'': 2.0, ''xF'': 1.0, ''xG'': 1.0, ''xS'': 1.0, ''nu'': 1.0, ''xT'':
3.1415000000000002}
Is this what the __setitem__ method is for? If not, how can I do what
I want to do?


>> class d(dict):
>>class d(dict):



... def __setitem __(self,k,v):

... print" Setting", k

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

...

... def __setitem__(self, k, v):
... print "Setting", k
... dict.__setitem__(self, k, v)
...


>> dd = d()
dd [''steve''] =''holden''
>>dd = d()
dd[''steve''] = ''holden''



设置steve

Setting steve


>> dd [''steve'']
>>dd[''steve'']



''holden''

''holden''


>>>
>>>



我认为问题是你的__new__方法没有返回xs类型的

对象但是一个字典,所以它不会从xs继承__getitem__

方法,而是从字典继承。


问候

Steve

-

Steve Holden +44 150 684 7255 +1 800 494 3119

Holden Web LLC / Ltd http://www.holdenweb.com

Skype:holdenweb http://del.icio.us/steve.holden

博客注: http://holdenweb.blogspot.com

见到你PYCON? http://us.pycon.org/TX2007


On6fév,16:23,jeremito < jerem ... @ gmail.comwrote:
On 6 fév, 16:23, "jeremito" <jerem...@gmail.comwrote:

如果这对其他人来说很明显,请原谅我,但我无法理解

out。我是dict的子类,但是想要防止直接更改

一些键/值对。为此,我认为我应该覆盖

__setitem__方法:


class xs(dict):

"" ;"

XS是一个容器对象,用于保存有关横截面的信息。

"""


def __new __(cls,xS = 1.0,xF = 1.0,xG = 1.0,nu = 1.0,debug = 0):

"""

"" ;"

x = {}

x [''xS''] = xS

x [''xF''] = xF

x [''nu''] = nu

x [''xG''] = xG

x [''xA''] = x [ ''xG''] + x [''xF'']

x [''xT''] = x [''xA''] + x [''xS'']


返回x
Please excuse me if this is obvious to others, but I can''t figure it
out. I am subclassing dict, but want to prevent direct changing of
some key/value pairs. For this I thought I should override the
__setitem__ method as such:

class xs(dict):
"""
XS is a container object to hold information about cross sections.
"""

def __new__(cls, xS=1.0, xF=1.0, xG=1.0, nu=1.0, debug=0):
"""
"""
x = {}
x[''xS''] = xS
x[''xF''] = xF
x[''nu''] = nu
x[''xG''] = xG
x[''xA''] = x[''xG''] + x[''xF'']
x[''xT''] = x[''xA''] + x[''xS'']

return x



将其替换为:

def __init __(self,xS = 1.0,xF = 1.0,xG = 1.0,nu = 1.0,debug = 0):

dict .__ init __(

self,

xS = xS,

xF = xF ,

xG = xG,

nu = nu,

xA = xG + xF,

xT = xG + xF + xS


replace this with:
def __init__(self, xS=1.0, xF=1.0, xG=1.0, nu=1.0, debug=0):
dict.__init__(
self,
xS=xS,
xF=xF,
xG=xG,
nu=nu,
xA=xG + xF,
xT=xG + xF + xS
)


def __setitem __(self,key,value):

"" ;"

我已经覆盖了这种方法,以防止在

级别之外设置xT或xA



"""

print"我在__setitem __"

if key ==''xT'':

引发AttributeError (
def __setitem__(self, key, value):
"""
I have overridden this method to prevent setting xT or xA
outside the
class.
"""
print "I am in __setitem__"
if key == ''xT'':
raise AttributeError(



不能改变xT。请更改,xF,xS或xG"



dict .__ setitem __(自我,键,值)

"Can''t change xT. Please change, xF, xS, or xG"
)
dict.__setitem__(self, key, value)


但我甚至无法运行__setitem__。
But I can''t even get __setitem__ to run.



当然,因​​为你的__new__方法返回一个dict实例,而不是xs

实例...

极少数情况下你真的需要覆盖__new__

方法。

of course, since your __new__ method returns a dict instance, not a xs
instance...
There are very few cases where you really need to override the __new__
method.


例如:

Python 2.5(r25:51918,Sep 19 2006,08:49:13)

[GCC 4.0.1(Apple Computer,Inc。build 5341)]在darwin上

输入帮助,版权,信用等。或许可证或有关更多信息。>>>导入xs
Example:
Python 2.5 (r25:51918, Sep 19 2006, 08:49:13)
[GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwin
Type "help", "copyright", "credits" or "license" for more information.>>>import xs

> cs = xs.xs()
cs
>cs = xs.xs()
cs



{''xA'':2.0,''xF'':1.0,''xG'':1.0,''xS'' :1.0,''nu'':1.0,''xT'':3.0}>> cs [''xT''] = 3.1415


{''xA'': 2.0, ''xF'': 1.0, ''xG'': 1.0, ''xS'': 1.0, ''nu'': 1.0, ''xT'': 3.0}>>cs[''xT''] = 3.1415


> cs
>cs



{''xA'':2.0,''xF'':1.0,''xG '':1.0,''xS'':1.0,''nu'':1.0,''xT'':

3.1415000000000002}


是这是__setitem__方法的用途?


{''xA'': 2.0, ''xF'': 1.0, ''xG'': 1.0, ''xS'': 1.0, ''nu'': 1.0, ''xT'':
3.1415000000000002}

Is this what the __setitem__ method is for?



是的。但请注意,你需要手动调用超类的

覆盖方法 - 除非你确实想要用你自己的替换它,这显然不是
这里的情况......


请注意,如果有人手动更改xG,xF或xS的值,则计算

xA和/或xT的值

不会反映这种变化。这就是你想要的吗?


最后,如果我可能会问,你的子类使用是什么?

dict?你不需要这个来实现类似dict的对象,

并且在你的情况下写一个普通的类可能更简单,然后

添加支持dict界面的必需子集。


我的2美分......

Yes. But note that you you need to manually call the superclass''s
overriden method - unless you
really want to replace it with your own, which is obviously not the
case here...

Note that if someone manually changes the values of xG, xF, or xS, the
computed values of xA and/or xT
won''t reflect this change. Is that what you want ?

Finally, and if I may ask, what is your use-case for subclassing
dict ? You don''t need this to implement a dict-like object,
and it might be simpler in your case to write an ordinary class, then
add support for the required subset of the dict interface.

My 2 cents...


2月6日,10: 59 am,bruno.desthuilli ... @ gmail.com

< bruno.desthuilli ... @ gmail.comwrote:
On Feb 6, 10:59 am, "bruno.desthuilli...@gmail.com"
<bruno.desthuilli...@gmail.comwrote:

On6fév,16:23,jeremito < jerem ... @ gmail.comwrote:
On 6 fév, 16:23, "jeremito" <jerem...@gmail.comwrote:

如果这对其他人来说很明显,请原谅我,但我无法理解

out。我是dict的子类,但是想要防止直接更改

一些键/值对。为此,我认为我应该覆盖

__setitem__方法:
Please excuse me if this is obvious to others, but I can''t figure it
out. I am subclassing dict, but want to prevent direct changing of
some key/value pairs. For this I thought I should override the
__setitem__ method as such:


class xs(dict):

"""

XS是一个容器对象,用于保存有关横截面的信息。

"""
class xs(dict):
"""
XS is a container object to hold information about cross sections.
"""


def __new __(cls,xS = 1.0,xF = 1.0,xG = 1.0,nu = 1.0,debug = 0):

"""

"""

x = {}

x [''xS''] = xS

x [''xF''] = xF

x [''nu''] = nu

x [''xG'' ] = xG

x [''xA''] = x [''xG''] + x [''xF'']

x [''xT''' ] = x [''xA''] + x [''xS'']
def __new__(cls, xS=1.0, xF=1.0, xG=1.0, nu=1.0, debug=0):
"""
"""
x = {}
x[''xS''] = xS
x[''xF''] = xF
x[''nu''] = nu
x[''xG''] = xG
x[''xA''] = x[''xG''] + x[''xF'']
x[''xT''] = x[''xA''] + x[''xS'']


返回x
return x



将其替换为:

def __init __(self,xS = 1.0,xF = 1.0,xG = 1.0,nu = 1.0,debug = 0):

dict .__ init __(

self,

xS = xS,

xF = xF,

xG = xG,

nu = nu,

xA = xG + x F,

xT = xG + xF + xS



replace this with:
def __init__(self, xS=1.0, xF=1.0, xG=1.0, nu=1.0, debug=0):
dict.__init__(
self,
xS=xS,
xF=xF,
xG=xG,
nu=nu,
xA=xG + xF,
xT=xG + xF + xS
)


def __setitem __(self,key,value ):

"""

我已覆盖此方法以防止在
$之外设置xT或xA

b $ b class。

"""

print"我在__setitem __"

if key ==''xT '':

引发AttributeError(
def __setitem__(self, key, value):
"""
I have overridden this method to prevent setting xT or xA
outside the
class.
"""
print "I am in __setitem__"
if key == ''xT'':
raise AttributeError(



不能改变xT。请更改,xF,xS或xG"



dict .__ setitem __(自我,键,值)


"Can''t change xT. Please change, xF, xS, or xG"
)
dict.__setitem__(self, key, value)


但我甚至无法运行__setitem__。
But I can''t even get __setitem__ to run.



当然,因​​为你的__new__方法返回一个dict实例,而不是xs

实例...

极少数情况下你真的需要覆盖__new__

方法。


of course, since your __new__ method returns a dict instance, not a xs
instance...
There are very few cases where you really need to override the __new__
method.



我用__new__而不是__init__创建对象的原因是

因为当我设置一个值时使用__init__它会调用__setitem__。

这是我想要发生的事情,但不是在__init__内部。这个

有意义吗?我确定有一个更好/更pythonic的方式来做到这一点,

但我不确定它是什么。有人能告诉我一个如何使用这个应该有效吗?

The reason I create my object with __new__ instead of __init__ is
because when I use __init__ when a value is set it calls __setitem__.
This is what I want to happen, but not inside of __init__. Does this
make sense? I''m sure there is a better/more pythonic way to do this,
but I''m unsure of what it is. Can someone show me an example of how
this should work?


>
>

示例:

Python 2.5(r25:51918,2006年9月19日,08:49:13)

[GCC 4.0.1(Apple Computer,Inc。build 5341)] on darwin

输入help,copyright,credit等。或许可证或有关更多信息。>>导入xs
Example:
Python 2.5 (r25:51918, Sep 19 2006, 08:49:13)
[GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwin
Type "help", "copyright", "credits" or "license" for more information.>>import xs

>> cs = xs.xs()

>> cs
>>cs = xs.xs()
>>cs


{''xA'':2.0,''xF'':1.0,''xG'':1.0,' 'xS'':1.0,''nu'':1.0,''xT'':3.0}>> cs [''xT''] = 3.1415
{''xA'': 2.0, ''xF'': 1.0, ''xG'': 1.0, ''xS'': 1.0, ''nu'': 1.0, ''xT'': 3.0}>>cs[''xT''] = 3.1415

>> cs
>>cs


{''xA'':2.0,''xF'':1.0,' 'xG'':1.0,''xS'':1.0,''nu'':1.0,''xT'':

3.1415000000000002}
{''xA'': 2.0, ''xF'': 1.0, ''xG'': 1.0, ''xS'': 1.0, ''nu'': 1.0, ''xT'':
3.1415000000000002}


这是__setitem__方法的用途吗?
Is this what the __setitem__ method is for?



是的。但请注意,你需要手动调用超类的

覆盖方法 - 除非你确实想要用你自己的替换它,这显然不是
这里的情况......


请注意,如果有人手动更改xG,xF或xS的值,则计算

xA和/或xT的值

不会反映这种变化。这是你想要的吗?


Yes. But note that you you need to manually call the superclass''s
overriden method - unless you
really want to replace it with your own, which is obviously not the
case here...

Note that if someone manually changes the values of xG, xF, or xS, the
computed values of xA and/or xT
won''t reflect this change. Is that what you want ?



最终(当我弄清楚如何使用__setitem__时)我将改变

当xG,xF时会发生什么,或xS被更改,以便它也改变xA

和xT。

Eventually (when I figure out how to use __setitem__) I will change
what happens when xG, xF, or xS are changed so that it also changes xA
and xT.


最后,如果我可能会问,你有什么用-case用于子类化

dict?你不需要这个来实现类似dict的对象,

并且在你的情况下写一个普通的类可能更简单,然后

添加支持dict接口的必需子集。
Finally, and if I may ask, what is your use-case for subclassing
dict ? You don''t need this to implement a dict-like object,
and it might be simpler in your case to write an ordinary class, then
add support for the required subset of the dict interface.



最后我要为我的班级添加其他功能(因为我已经提到了
)所以我不能简单地使用dict对象。

Eventually I am going to add other features to my class (as I have
mentioned) so I can''t simply use a dict object.


>

我的2美分...
>
My 2 cents...



再次感谢,

Jeremy

Thanks again,
Jeremy


这篇关于如何使用dict对象的__setitem__方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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