Why does the decorator property indicate that "the object has no attribute"?

I have the following code:

import sys
import platform
from PyQt4.QtGui import QApplication
from PyQt4.QtWebKit import QWebPage

class Render(QWebPage):
    def __init__(self):
        self.app = QApplication([])
        QWebPage.__init__(self)

    @property
    def html(self):
        return self.mainFrame().toHtml.toAscii()

page = Render()
print sys.version, platform.platform()
print 'html attribute?', [p for p in dir(page) if 'html' in p]
print page.html

gives this exception conclusion:

stav@maia:$ python property.py
2.7.3 (default, Aug  1 2012, 05:14:39)
[GCC 4.6.3] Linux-3.2.0-38-generic-x86_64-with-Ubuntu-12.04-precise
html attribute? ['html']
Traceback (most recent call last):
  File "property.py", line 18, in <module>
    print page.html
AttributeError: 'Render' object has no attribute 'html'

If I delete the decoder @propertyor I delete the call .toAscii, then it will work. But why does the error say that there is no attribute, even tho dir(page)shows it?

+5
source share
2 answers

The problem is that Python gave an erroneous error message. The error message that might be expected in this case is as follows:

AttributeError: 'function' object has no attribute 'toAscii'

But instead, Python gave an erroneous error message:

AttributeError: 'Render' object has no attribute 'html'

That is, the AttributeErrorone generated inside the property function was presented as if it were AttributeErrorfor the property itself.

, @property QObject. PyQt. , PyQt , (, IMHO). . . ( , QObject object Python, .)

+5

, .toHtml().toAscii(). .

+1

All Articles