我有一个班级和一些方法
class ThisClass:
def method1(self):
text1 = 'iloveyou'
return text1
def method2(self):
text2 = self.method1
print str(text2)
thisObj = ThisClass()
thisObj.method2
Run Code Online (Sandbox Code Playgroud)
我得到的结果是类似的
<bound method thisclass.method2 of <__main__.thisclass instance at 0x10042eb90>>
Run Code Online (Sandbox Code Playgroud)
我如何打印'iloveyou'而不是那个东西?
谢谢!
缺少方法调用的().如果没有(),则打印方法对象的字符串表示形式,对于包括自由函数在内的所有可调用对象也是如此.
确保为所有方法调用执行此操作(self.method1和thisObj.method2)
class ThisClass:
def method1(self):
text1 = 'iloveyou'
return text1
def method2(self):
text2 = self.method1()
print str(text2)
thisObj = ThisClass()
thisObj.method2()
Run Code Online (Sandbox Code Playgroud)