0 python
如果您采用以下简单类:
class AltString:
def __init__(self, str = "", size = 0):
self._contents = str
self._size = size
self._list = [str]
def append(self, str):
self._list.append(str)
def output(self):
return "".join(self._list)
Run Code Online (Sandbox Code Playgroud)
我使用以下方法成功调用了类实例:
as = AltString("String1")
as.append("String2")
as.append("String3")
Run Code Online (Sandbox Code Playgroud)
当我然后output使用as.output而不是返回字符串来调用函数时,我得到以下内容:
unbound method AltString.output
Run Code Online (Sandbox Code Playgroud)
如果我使用它as.output()我得到以下错误:
TypeError: unbound method output() must be called with
AltString instance as first argument (got nothing instead)
Run Code Online (Sandbox Code Playgroud)
我做得对不对?
as是一个错误的变量名,它是Python中的保留关键字.不要像这样命名你的变量.一旦你解决了,其他一切都会好起来的.当然你应该这样做:
alt_str.output()
Run Code Online (Sandbox Code Playgroud)
编辑:我在尝试申请output课时能够复制你的错误信息:AltString.output,然后:AltString.output().您应该将该方法应用于该类的实例.
alt_str = AltString('spam')
alt_str.output()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
226 次 |
| 最近记录: |