Tôi có đoạn mã sau:Tại sao trình bày trang thuộc tính hiển thị "đối tượng không có thuộc tính"?
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
cho đầu ra ngoại lệ này:
[email protected]:$ 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'
Nếu tôi loại bỏ các @property
trang trí hoặc tôi loại bỏ các .toAscii
cuộc gọi, sau đó nó hoạt động. Nhưng tại sao lỗi nói rằng không có thuộc tính, ngay cả tho dir(page)
cho thấy nó?
* Ngoài *: Có thể bạn có nghĩa là '.toHtml(). ToAscii()'. Lưu ý các dấu ngoặc đơn còn thiếu. –
Thuộc tính chỉ khả dụng đối với các đối tượng Python xuất phát từ 'đối tượng' – dawg
Bạn đúng @ Robᵩ! ... bạn nên gửi câu trả lời, đó là nó. –