【发布时间】:2015-02-17 10:02:22
【问题描述】:
代码有什么问题? Python 在方法返回类字符串时返回 TypeError。
class window:
def __init__(self, title='window'):
self.title = title
def title(self, title):
if title:
self.title = title
else:
return self.title
window = window()
window.title('changed')
print(window.title())
错误:
Traceback (most recent call last):
File "C:/Users/Danilo/Desktop/pygtk.py", line 10, in <module>
window.title('changed')
TypeError: 'str' object is not callable
【问题讨论】:
-
我怀疑这与您使用
title作为变量和函数的名称有关
标签: python string class methods return