2010-08-09 70 views
1

過去一週我一直在玩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

我已經和繼續在研究的過程中問題。

任何幫助,這將不勝感激。 謝謝!

+0

格式注:請縮進代碼段和等4位。謝謝! – 2010-08-09 19:10:08

+0

確保讓你的類從'object'繼承。如果你不這樣做,你會使用舊式的課程,那很糟糕。 – Daenyth 2010-08-09 19:14:43

+0

另一件需要注意的是:這些函數是實例方法,而不是類方法。 – Daenyth 2010-08-09 19:15:41

回答

5

你缺少自我在你的定義

當一個類的方法被稱爲蟒包括的對象引用作爲第一個函數參數

def setLineCoordinates(self,x0,y0,x1,y1) 
def getLineCoordinatesX0(self): 
... 
+0

好的! 我會嘗試一下。謝謝你的幫助!!! – 2010-08-09 19:07:34

+0

這個伎倆! 謝謝! – 2010-08-09 19:09:54

+2

@Michael:你應該接受答案(旁邊的複選標記) – Daenyth 2010-08-09 19:15:03

2

你忘了自己的方法名。

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++那樣是隱含的)。

4

您忘了將self添加到類方法的定義中。第一個參數(按照慣例稱爲self)包含對對象實例的引用,它需要明確地寫在定義中,而當方法被調用時它將被隱式添加。

def setLineCoordinates(self, locx0, locy0, locx1, locy1): 
    etc... 

應該工作得更好。

1

你的函數定義FYI缺少`自

def setLineCoordinates(self, locx0, locy0, locx1, locy1): 
    self.locx0 = locx0 
    .... 
3

- 這是實例方法,而不是一個類的方法。 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']