property() bug

Tim Peters tim.one at comcast.net
Mon Oct 14 03:13:57 EDT 2002


[Chuck Esterbrook]
> I found a bug in both 2.2.2b1 and 2.2.1.

Nope.  Proof:  If you had, I would be panicking <wink>.

> Looking through the  SourceForge Bugs Summaries, I didn't see it
> listed. Anyone have any  comments on this before I add it to the list?
>
> ------------------------------------------------------------------------
> class Good:
>     def foo(self):
>         # raises IOError as expected
>         return open('lsadflkdsfkjsdf').read()
> try:
>     print Good().foo()
> except IOError, e:
>     pass  # as expected
>
>
> class Bad:

That's your problem.  Properties are a new-in-2.2-feature that require
new-style classes in order to work as intended, and you're defining an
old-style ("classic") class.  Change this to

    class Bad(object):

and it will work fine.  You could also do

    class Bad:
        __metaclass__ = type

to force a new-style class without inheriting from object, or even put

    __metaclass__ = type

near the top of the module to force *all* classes in the module to be
new-style.  Those are (IMO) more obscure, though.





More information about the Python-list mailing list