将参数传递给Python中的类方法的问题
过去一周我一直在玩Python,并遇到将4个参数传递给类方法的问题。将参数传递给Python中的类方法的问题
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
这里就是我称之为类方法:
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
我已经和继续在研究的过程中问题。
任何帮助,这将不胜感激。 谢谢!
你缺少自我在你的定义
当一个类的方法被称为蟒包括的对象引用作为第一个函数参数
def setLineCoordinates(self,x0,y0,x1,y1)
def getLineCoordinatesX0(self):
...
好的! 我会尝试一下。谢谢你的帮助!!! – 2010-08-09 19:07:34
这个伎俩! 谢谢! – 2010-08-09 19:09:54
@Michael:你应该接受答案(旁边的复选标记) – Daenyth 2010-08-09 19:15:03
你忘了自己的方法名。
class Line():
def setLineCoordinates(self,locx0, locy0, locx1, locy1):
self.locx0 = locx0
self.locy0 = locy0
self.locx1 = locx1
self.locy1 = locy1
def getLineCoordinatesX0(self):
return self.x0
在python中,自变量的传递必须是显式的(它不像C++那样是隐含的)。
您忘了将self
添加到类方法的定义中。第一个参数(按照惯例称为self
)包含对对象实例的引用,它需要明确地写在定义中,而当方法被调用时它将被隐式添加。
def setLineCoordinates(self, locx0, locy0, locx1, locy1):
etc...
应该工作得更好。
你的函数定义FYI缺少`自
def setLineCoordinates(self, locx0, locy0, locx1, locy1):
self.locx0 = locx0
....
- 这是实例方法,而不是一个类的方法。 python中有三种方法类型。
- 甲类方法是接收
class
对象作为第一个参数的方法 - 甲静态方法是绝对没有上下文的方法
- 一种实例方法是一种方法,其接收作为第一个参数的
class
的实例
每种方法类型都有一个不同的用途和目的。正如其他人所说的,您缺少实例方法的self
实例参数。下面是每种方法类型的一个小例子,以及它们如何被调用来进行比较。
class C:
c_class_var = 1
def __init__(self):
self.instance_var = 2
def instance_method(self):
print
print 'instance_method called for object', self
print 'dir()', dir()
print 'dir(self)', dir(self)
@staticmethod
def static_method():
print
print 'static_method called, no context exists!'
print 'dir()', dir()
@classmethod
def class_method(cls):
print
print 'class_method called for class', cls
print 'dir()', dir()
print 'dir(cls)', dir(cls)
class D(C):
d_class_var = 3
c_obj = C()
c_obj.instance_method()
C.static_method()
C.class_method()
d_obj = D()
d_obj.instance_method()
D.static_method()
D.class_method()
这里是输出:
instance_method called for object <__main__.C instance at 0x00A706E8>
dir() ['self']
dir(self) ['__doc__', '__init__', '__module__', 'c_class_var', 'class_method', 'instance_method', 'instance_var', 'static_method']
static_method called, no context exists!
dir() []
class_method called for class __main__.C
dir() ['cls']
dir(cls) ['__doc__', '__init__', '__module__', 'c_class_var', 'class_method', 'instance_method', 'static_method']
instance_method called for object <__main__.D instance at 0x00A70710>
dir() ['self']
dir(self) ['__doc__', '__init__', '__module__', 'c_class_var', 'class_method', 'd_class_var', 'instance_method', 'instance_var', 'static_method']
static_method called, no context exists!
dir() []
class_method called for class __main__.D
dir() ['cls']
dir(cls) ['__doc__', '__init__', '__module__', 'c_class_var', 'class_method', 'd_class_var', 'instance_method', 'static_method']
格式注:请缩进代码段和等4位。谢谢! – 2010-08-09 19:10:08
确保让你的类从'object'继承。如果你不这样做,你会使用旧式的课程,那很糟糕。 – Daenyth 2010-08-09 19:14:43
另一件需要注意的是:这些函数是实例方法,而不是类方法。 – Daenyth 2010-08-09 19:15:41