@property decorator doesn't raise exceptions

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sat Oct 25 01:13:39 CEST 2008

On Fri, 24 Oct 2008 09:34:36 -0700, Rafe wrote:

>> 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?

Only in Python 3. If you are relying on that to be true in Python 2.x, 
you're going to disappointed:

>>> class A():
...     pass
>>> issubclass(A, object)

There are a lot of classic classes in the standard library. If you 
inherit from them, your class will also be a classic class and properties 
will fail to work correctly.

Earlier, you wrote:

"I've encountered a problem which is making debugging less obvious than
it should be. The @property decorator doesn't always raise exceptions."

Are you expecting it to raise an exception when the class is defined, or 
when the property is called? e.g.

class Fail(object):
    "this should raise an exception"

Works for me -- I get a syntax error, as expected.

"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__."

Perhaps you can give us a minimal example showing the code with the bug, 
then how you corrected the bug? That might give as a hint as to what is 
going on. I know that's hard, when you can't reproduce the problem, but 
it's just as hard for us to solve your problem without more information.


More information about the Python-list mailing list