不受约束的方法错误
问题描述:
Class test:
a=10
b=20
Def c(self,a,b):
Return a+b
Print test.a
#10
Print test.b
#20
Print test.c(1,2)
#Error unbound method c()
PLZ指出是我错了,我初学者而言使用类的不受约束的方法错误
对不起,在我的代码。我在上例,键入它在我的小手机屏幕。
答
# class is lowercase
# class names are by convention uppercase (Test)
class Test:
# def is lowercase
def c(self, a, b):
# return is lowercase
return a + b
# Create a new instance of the Test class
test = Test()
print test.c(1, 2) # prints 3
答
您试图直接访问类内的方法。
要调用方法类中需要创建类
的情况下编程将工作
class Test:
a = 10 # class variable
b = 20 # class variable
def c(self,a,b):
return a+b
print Test.a
print Test.b
obj = Test()
print obj.c(1,2) #3
你拼错'class','def','print'和'return'。并且您标记了Python 2.7和Python 3.x.这是什么? – smarx
您需要创建一个'test'的新实例来使用它的方法。 – smarx
这不是Python 3,这是肯定的 –