设置属性而不调用__setattr __() [英] Setting an attribute without calling __setattr__()

查看:65
本文介绍了设置属性而不调用__setattr __()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我肯定答案是正确地盯着我看 - 无论那个答案是否是b $ b b,你不能这样做。或者这里是非常简单的方式 - 但我被卡住了。我是

写一个对象代理两个列表(可订阅的iterables,真的)和

dicts。


我的初始化看起来像这样:


def __init __(self,obj = None):

如果在''list | tuple | set | frozenset中输入类型(obj).__ name__ '':

self.me = []

for v in obj:

self.me.append(ObjectProxy(v))

elif type(obj)== dict:

self.me = {}

代表k,v代表obj.items():

self.me [k] = ObjectProxy(v)


我有一个__setattr__定义如下:


def __setattr __(self,name,value):

self.me [name] = ObjectProxy(value)


你可能会看到问题。


在执行init时,self.me = {}或self.me = []调用__setattr __,然后

结束无限循环,并且即使它成功了


self.me [''我''] = {}


不是我想要的第一名。


有没有办法定义self.me而不激活__setattr__?


如果没有,这不是一个大问题,因为现在这个班级只读了 t $ / b $ b是一个问题,但我只是想让它读/写。


谢谢!


j

解决方案

Joshua Kugler< jk ***** @ bigfoot.comwrites:


self.me = []

self.me = {}



使用object .__ setattr __(自我,''我'')= []同样适用于{}。


4月26日上午7:01,Joshua Kugler< jkug ... @ bigfoot.comwrote:


好​​的,我肯定答案是正确地盯着我看 - 无论那个答案是否是b $ b b,你不能那样做。或者这里是非常简单的方式 - 但我被卡住了。我是

写一个对象代理两个列表(可订阅的iterables,真的)和

dicts。


我的初始化看起来像这样:


def __init __(self,obj = None):

如果在''list | tuple | set | frozenset中输入类型(obj).__ name__ '':

self.me = []

for v in obj:

self.me.append(ObjectProxy(v))

elif type(obj)== dict:

self.me = {}

代表k,v代表obj.items():

self.me [k] = ObjectProxy(v)


我有一个__setattr__定义如下:


def __setattr __(self,name,value):

self.me [name] = ObjectProxy(value)


你可能会看到问题。


在执行init时,self.me = {}或self.me = []调用__setattr __,然后

结束无限循环,并且即使它成功了


self.me [''我''] = {}


首先不是我想要的。


有没有如何定义self.me而不激活__setattr__?



考虑阅读关于__setattr__的* second *段落

3.4.2 of Python参考手册。


Hrvoje Niksic< hn ***** @ xemacs.orgwrites:


Joshua Kugler< jk ***** @ bigfoot.comwrites:


> self.me = []
self.me = {}



使用" object .__ setattr __(self,''me'')= [] "同样适用于{}。



哎呀,当然应该是object .__ setattr __(self,''me'',[])"。


OK, I''m sure the answer is staring me right in the face--whether that answer
be "you can''t do that" or "here''s the really easy way--but I am stuck. I''m
writing an object to proxy both lists (subscriptable iterables, really) and
dicts.

My init lookslike this:

def __init__(self, obj=None):
if type(obj).__name__ in ''list|tuple|set|frozenset'':
self.me = []
for v in obj:
self.me.append(ObjectProxy(v))
elif type(obj) == dict:
self.me = {}
for k,v in obj.items():
self.me[k] = ObjectProxy(v)

and I have a __setattr__ defined like so:

def __setattr__(self, name, value):
self.me[name] = ObjectProxy(value)

You can probably see the problem.

While doing an init, self.me = {} or self.me = [] calls __setattr__, which
then ends up in an infinite loop, and even it it succeeded

self.me[''me''] = {}

is not what I wanted in the first place.

Is there a way to define self.me without it firing __setattr__?

If not, it''s not a huge deal, as having this class read-only for now won''t
be a problem, but I was just trying to make it read/write.

Thanks!

j

解决方案

Joshua Kugler <jk*****@bigfoot.comwrites:

self.me = []
self.me = {}

Use "object.__setattr__(self, ''me'') = []" and likewise for {}.


On Apr 26, 7:01 am, Joshua Kugler <jkug...@bigfoot.comwrote:

OK, I''m sure the answer is staring me right in the face--whether that answer
be "you can''t do that" or "here''s the really easy way--but I am stuck. I''m
writing an object to proxy both lists (subscriptable iterables, really) and
dicts.

My init lookslike this:

def __init__(self, obj=None):
if type(obj).__name__ in ''list|tuple|set|frozenset'':
self.me = []
for v in obj:
self.me.append(ObjectProxy(v))
elif type(obj) == dict:
self.me = {}
for k,v in obj.items():
self.me[k] = ObjectProxy(v)

and I have a __setattr__ defined like so:

def __setattr__(self, name, value):
self.me[name] = ObjectProxy(value)

You can probably see the problem.

While doing an init, self.me = {} or self.me = [] calls __setattr__, which
then ends up in an infinite loop, and even it it succeeded

self.me[''me''] = {}

is not what I wanted in the first place.

Is there a way to define self.me without it firing __setattr__?

Consider reading the *second* paragraph about __setattr__ in section
3.4.2 of the Python Reference Manual.


Hrvoje Niksic <hn*****@xemacs.orgwrites:

Joshua Kugler <jk*****@bigfoot.comwrites:

> self.me = []
self.me = {}


Use "object.__setattr__(self, ''me'') = []" and likewise for {}.

Oops, that should of course be "object.__setattr__(self, ''me'', [])".


这篇关于设置属性而不调用__setattr __()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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