在过去的一周里,我一直在玩Python,在向类方法传递4个参数时遇到了一个问题。你知道吗
下面是在其类中定义的类方法:
class Line:
locx0 = 0
locy0 = 0
locx1 = 0
locy1 = 0
def __init__(self):
print'<<Line __init__()>>'
def setLineCoordinates(locx0, locy0, locx1, locy1):
self.locx0 = locx0
self.locy0 = locy0
self.locx1 = locx1
self.locy1 = locy1
def getLineCoordinatesX0():
return self.x0
def getLineCoordinatesY0():
return self.y0
def getLineCoordinatesX1():
return self.x1
def getLineCoordinatesY0():
return self.y0
我在这里调用class方法:
def LineDepot():
x0 = None
x1 = None
y0 = None
y1 = None
line = Line()
print"Please enter starting and ending coordinates "
print"If no value is entered, then it will be assumed that the coordinate value is zero "
x0 = int(input('Enter value for initial x coordiante : '))
y0 = int(input('Enter value for initial y coordiante : '))
x1 = int(input('Enter value for end x coordiante :'))
y1 = int(input('Enter value for end y coordiante :'))
line.setLineCoordinates(x0, y0, x1, y1)
这是我在输出中遇到的错误:
Please make a selection from the following menu...
1.Create a new Line
2.View current lines
3.View logs
4.Mail line info or logs
5.View summary of line stats
6.Exit this program
Menu Selection:1
<<Line __init__()>>
Please enter starting and ending coordinates
If no value is entered, then it will be assumed that the coordinate value is zero
Enter value for initial x coordiante : 1
Enter value for initial y coordiante : 2
Enter value for end x coordiante :3
Enter value for end y coordiante :4
Traceback (most recent call last):
File "./linear.line.py", line 107, in <module>
Main()
File "./linear.line.py", line 15, in Main
Menu()
File "./linear.line.py", line 52, in Menu
LineDepot()
File "./linear.line.py", line 32, in LineDepot
line.setLineCoordinates(x0, y0, x1, y1)
TypeError: setLineCoordinates() takes exactly 4 arguments (5 given)
我一直想弄明白为什么当我通过4个参数时,翻译是 告诉我我想通过5级考试。你知道吗
我已经并将继续研究这个问题。你知道吗
任何帮助都将不胜感激。 谢谢!!!你知道吗
仅供参考-这是一个实例方法,而不是类方法。python中有三种方法类型。你知道吗
class
对象作为第一个参数的方法class
实例作为第一个参数的方法每种方法类型都有不同的用途和目的。正如其他人所说,您的实例方法缺少
self
实例参数。下面是每个方法类型的一个小示例,以及如何调用它们进行比较。你知道吗下面是输出:
在你的定义中你失去了自我
调用类方法时,python将对象引用作为第一个函数参数
您忘了将
self
添加到类方法的定义中。第一个参数(按惯例称为self
)包含对对象实例的引用,它需要显式地写入定义中,而在调用方法时它将隐式地添加。你知道吗应该更有效。你知道吗
相关问题 更多 >
编程相关推荐