Issue with passing paramters to a class method in Python
I have been playing with Python for the past week and I running into a problem with passing 4 parameters to a class method.
Here is the class method defined within it's class:
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
Here is where I call the class method:
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)
This is the error I have been getting in the output :
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 "./lin开发者_Go百科ear.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)
I trying to figure out for the life of me why when I pass 4 arguments, the interpreter is telling me that I am trying to pass 5.
I have and continue to be in the process of researching the problem.
Any help with this will be greatly appreciated. Thanks!!!
You are missing self in your definition
When a class method is called python includes the objects reference as the first function argument
def setLineCoordinates(self,x0,y0,x1,y1)
def getLineCoordinatesX0(self):
...
You forgot to add self
to the class methods' definitions. The first parameter (which by convention is called self
) contains a reference to the object instance, and it needs to be explicitly written in the definition, whereas it will be implicitly added when the method is called.
def setLineCoordinates(self, locx0, locy0, locx1, locy1):
etc...
should work better.
FYI - that is an instance method and not a class method. There are three method types in python.
- A class method is a method that receives a
class
object as the first argument - A static method is a method with absolutely no context
- An instance method is a method that receives a instance of a
class
as the first argument
Each of the method types has a different use and purpose. As others have stated, you are missing the self
instance argument to your instance methods. Here's a little example of each of the method types and how they are called for comparison.
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()
and here is the output:
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']
You forgot to put self in the method name.
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
In python the passing of the self variable has to be explicit (it is not implicit like C++).
Your function definition is missing `self
def setLineCoordinates(self, locx0, locy0, locx1, locy1):
self.locx0 = locx0
....
精彩评论