<分区>
Possible Duplicate:
Static methods in Python?
我认为我的问题很简单,但更清楚地说,我只是想知道,我有这个:
class MyBrowser(QWebPage):
''' Settings for the browser.'''
def __init__(self):
QWebPage.__init__(self)
pass
def userAgentForUrl(self, url=None):
''' Returns a User Agent that will be seen by the website. '''
return "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.15 (KHTML, like Gecko) Chrome/24.0.1295.0 Safari/537.15"
在不同类中的某些地方,即在同一个文件中,我想获取此用户代理。
mb = MyBrowser()
user_agent = mb.userAgentForUrl()
print user_agent
我正在尝试做这样的事情:
print MyBrowser.userAgentForUrl()
但是出现了这个错误:
TypeError: unbound method userAgentForUrl() must be called with MyBrowser instance as first argument (got nothing instead)
所以我希望您能理解我的要求,有时我不想创建一个实例,也不想从这种函数中检索数据。所以问题是可以做还是不可以,如果可以,请给我一些关于如何实现这一目标的指导。