@property装饰器不会引发异常 [英] @property decorator doesn't raise exceptions

查看:70
本文介绍了@property装饰器不会引发异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我遇到了一个问题,这个问题使调试变得不那么明显了。

应该是这样。 @property装饰器并不总是引发异常。

看起来它与类绑定但在调用时被忽略。我可以

在一个实例上使用dir(self .__ class__)查看属性,但是当调用

时,python进入__getattr__。如果我更正错误,属性

调用按预期工作,不要调用__getattr__。


我似乎不能做一个简单的repro。任何人都可以提供任何线索,因为这可能会导致这种情况,所以我可以尝试证明这一点吗?

干杯,


- Rafe

解决方案

Rafe写道:




我遇到了一个问题,这个问题使调试不那么明显,而不是它应该是什么。 @property装饰器并不总是引发异常。

看起来它与类绑定但在调用时被忽略。我可以

在一个实例上使用dir(self .__ class__)查看属性,但是当调用

时,python进入__getattr__。如果我更正错误,属性

调用按预期工作,不要调用__getattr__。


我似乎不能做一个简单的repro。任何人都可以提供任何关于

的线索可能会导致这种情况,所以我可以尝试证明这一点吗?



你必须从对象中继承子类获得一个新的风格类。物业

在老式班上不能正常工作。


Christian


< blockquote> 10月24日凌晨2:21,Christian Heimes< li ... @ cheimes.dewrote:


Rafewrote:




我遇到了一个问题,这使得调试不如
$ b应该是$ b。 @property装饰器并不总是引发异常。

看起来它与类绑定但在调用时被忽略。我可以

在一个实例上使用dir(self .__ class__)查看属性,但是当调用

时,python进入__getattr__。如果我更正了错误,属性

调用按预期工作,不要调用__getattr__。


我似乎无法做出简单的复制。任何人都可以提供任何关于

的线索可能会导致这种情况,所以我可以尝试证明这一点吗?



你必须从对象中继承子类获得一个新的风格类。物业

在旧式班上不能正常工作。


Christian



全部类是对象的子类。还有其他想法吗?


- Rafe


Rafe写道:


10月24日凌晨2:21,Christian Heimes< li ... @ cheimes.dewrote:


> Rafewrote:




我遇到的问题是调试不如
应该是
。 @property装饰器并不总是引发异常。

看起来它与类绑定但在调用时被忽略。我可以

在一个实例上使用dir(self .__ class__)查看属性,但是当调用

时,python进入__getattr__。如果我更正了错误,属性

调用按预期工作,不要调用__getattr__。


我似乎无法做出简单的复制。任何人都可以提供任何关于

的线索可能会导致这种情况,所以我可以尝试证明这一点吗?


你必须从对象中继承子类。获得一个新的风格类。属性
在老式课程上不能正常工作。

Christian



所有类都是对象的子类。还有其他想法吗?



很难说你什么时候不给任何代码。

< blockquote class =post_quotes>
>> A类(对象):



.... @属性

.... def属性(个体经营):

....提升属性错误

.... def __getattr __(自我,姓名):

....返回没人希望西班牙宗教裁判所

....


>> A()。attribute



''没人希望西班牙宗教裁判所''


你的意思是这样的吗?我不认为__getattr __()调用可以在这里避免



Peter


Hi,

I''ve encountered a problem which is making debugging less obvious than
it should be. The @property decorator doesn''t always raise exceptions.
It seems like it is bound to the class but ignored when called. I can
see the attribute using dir(self.__class__) on an instance, but when
called, python enters __getattr__. If I correct the bug, the attribute
calls work as expected and do not call __getattr__.

I can''t seem to make a simple repro. Can anyone offer any clues as to
what might cause this so I can try to prove it?
Cheers,

- Rafe

解决方案

Rafe wrote:

Hi,

I''ve encountered a problem which is making debugging less obvious than
it should be. The @property decorator doesn''t always raise exceptions.
It seems like it is bound to the class but ignored when called. I can
see the attribute using dir(self.__class__) on an instance, but when
called, python enters __getattr__. If I correct the bug, the attribute
calls work as expected and do not call __getattr__.

I can''t seem to make a simple repro. Can anyone offer any clues as to
what might cause this so I can try to prove it?

You must subclass from "object" to get a new style class. properties
don''t work correctly on old style classes.

Christian


On Oct 24, 2:21 am, Christian Heimes <li...@cheimes.dewrote:

Rafewrote:

Hi,

I''ve encountered a problem which is making debugging less obvious than
it should be. The @property decorator doesn''t always raise exceptions.
It seems like it is bound to the class but ignored when called. I can
see the attribute using dir(self.__class__) on an instance, but when
called, python enters __getattr__. If I correct the bug, the attribute
calls work as expected and do not call __getattr__.

I can''t seem to make a simple repro. Can anyone offer any clues as to
what might cause this so I can try to prove it?


You must subclass from "object" to get a new style class. properties
don''t work correctly on old style classes.

Christian

All classes are a sub-class of object. Any other ideas?

- Rafe


Rafe wrote:

On Oct 24, 2:21 am, Christian Heimes <li...@cheimes.dewrote:

>Rafewrote:

Hi,

I''ve encountered a problem which is making debugging less obvious than
it should be. The @property decorator doesn''t always raise exceptions.
It seems like it is bound to the class but ignored when called. I can
see the attribute using dir(self.__class__) on an instance, but when
called, python enters __getattr__. If I correct the bug, the attribute
calls work as expected and do not call __getattr__.

I can''t seem to make a simple repro. Can anyone offer any clues as to
what might cause this so I can try to prove it?


You must subclass from "object" to get a new style class. properties
don''t work correctly on old style classes.

Christian


All classes are a sub-class of object. Any other ideas?

Hard to tell when you don''t give any code.

>>class A(object):

.... @property
.... def attribute(self):
.... raise AttributeError
.... def __getattr__(self, name):
.... return "nobody expects the spanish inquisition"
....

>>A().attribute

''nobody expects the spanish inquisition''

Do you mean something like this? I don''t think the __getattr__() call can be
avoided here.

Peter


这篇关于@property装饰器不会引发异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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