摆脱递归调用__getattr__ [英] Get rid of recursive call __getattr__

查看:143
本文介绍了摆脱递归调用__getattr__的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果

i需要使用类的方法或属性,我如何摆脱递归调用__getattr__?

How can I get rid of recursive call __getattr__ inside this method, if
i need to use method or property of the class?

推荐答案

Pelmen写道:
如何在这个方法中摆脱递归调用__getattr__,如果我需要使用类的方法或属性?
How can I get rid of recursive call __getattr__ inside this method, if
i need to use method or property of the class?



对不起,我不明白你的问题。哪些递归调用

__getattr__? __getattr__只有在定义了它/ b /

属性时才被调用(见下文)。


你有没有重写__setattr__还是__getattribute__?如果是,请阅读

精细手册的相应部分。


Sorry, but I don''t understand your question. Which recursive calls to
__getattr__ ? __getattr__ is only called if a/ it''s defined and b/ the
attribute has not been found (see below).

Have you overriden __setattr__ or __getattribute__ ? If yes, please read
the corresponding sections of the Fine Manual.

类Toto(对象):
.... def __init __(自我,名字):

.... self.name = name

.... def __getattr __(self,attname):

.... print" __ getattr__ for%s" %attname

....返回%s不存在 %attname

.... t = Toto(''toto'')
t.name = name
Traceback(最近一次调用最后一次):

文件"< stdin>",第1行,在?

NameError:名称''name''未定义t.name
''toto''t .age
class Toto(object): .... def __init__(self, name):
.... self.name = name
.... def __getattr__(self, attname):
.... print "__getattr__ called for %s" % attname
.... return "%s doesn''t exists" % attname
.... t = Toto(''toto'')
t.name = name Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name ''name'' is not defined t.name ''toto'' t.age



__getattr__年龄要求

年龄不存在

-

bruno desthuilliers

python -c" print''@''。join([''。''。join([w p.split(''。'')中的w的[:: - 1]])''o **** @ xiludom.gro'中的

p。分裂(''@ '')])"


__getattr__ called for age
"age doesn''t exists"

--
bruno desthuilliers
python -c "print ''@''.join([''.''.join([w[::-1] for w in p.split(''.'')]) for
p in ''o****@xiludom.gro''.split(''@'')])"




Pelmen写道:

Pelmen wrote:
如何摆脱递归调用__getattr__在这个方法里面,如果我需要使用类的方法或属性?
How can I get rid of recursive call __getattr__ inside this method, if
i need to use method or property of the class?




嗨Pelmen,


阅读了我的Python发行版附带的文档

__getattr__,我不知道还没看到你将如何获得

方法的递归调用...(仅当通过

正常意味着无法查找属性时才会调用它... )


如果你看到__getattr__的递归调用,也许你可以用一些示例代码突出显示问题吗?


问候,


--Tim



Hi Pelmen,

Having read the docs included with my Python distribution on
__getattr__, I don''t see yet how you will get recursive calls to the
method... (It''s called only when the attribute cannot be looked up via
normal means)

If you are seeing recursive calls to __getattr__, perhaps you can
highlight the problem with some sample-code?

regards,

--Tim


谢谢,我应该仔细阅读

thanks, i should been read more closely


这篇关于摆脱递归调用__getattr__的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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