我正在尝试创建一个具有多个函数的类,接受一个字符串作为参数并打印此字符串。到目前为止,这是我写的:
class test():
def func(self,text):
print str(text)
test.func('This is a bunch of text')
我收到以下错误消息:
_Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unbound method func() must be called with test instance as first argument (got str instance instead)_
有什么建议吗?
答案 0 :(得分:5)
您需要在调用实例方法之前实例化一个类:
class Test(object):
def func(self, text):
print str(text)
test = Test()
test.func('This is a bunch of text')
答案 1 :(得分:2)
或者你可以尝试 -
class test():
@staticmethod
def func(text):
print str(text)
test.func('This is a bunch of text')