【Python】出现bound method 错误

Python调用类方法时的常见错误

错误提示:
<bound method Mlei.GetInfo of <__main__.Mlei object at 0x00000131F0235EB0>>

源代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Mlei:
name="Leya"
sex="woman"
__age=25
__weight=50
def GetInfo(self):
return print("name:%s,sex:%s"%(self.name,self.sex))
def __GetMoreInfo(self):
return print("age:%s,weight:%s"%(self.__age,self.__weight))
c1=Mlei()
c2=Mlei()
print("访问公有类属性:",c1.name,c1.sex)
print("访问公有类方法:",end=' ')
c1.GetInfo

运行结果:

1
2
访问公有类属性: Leya woman
访问公有类方法: <bound method Mlei.GetInfo of <__main__.Mlei object at 0x00000131F0235EB0>>

错误原因:调用方法时,没有加上括号,正确调用方法c1.GetInfo()