2010-08-09 4 views
1

나는 지난 주 Python을 사용 해왔고 클래스 메서드에 4 개의 매개 변수를 전달할 때 문제가 발생했습니다.파이썬에서 클래스 메서드에 매개 변수를 전달할 때의 문제

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) 

이 내가 점점 된 오류는 다음과 같습니다 I 클래스의 메서드를 호출 할 경우

다음
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 

은 다음과 같습니다

클래스의 방법은 그것의 클래스 내에서 정의된다 출력 :

내가 사 개 인수를 통과 할 때

940,593,210 내가 내 인생 알아 내려고 노력하는 이유, 인터프리터 내가이 5

를 전달하는 데 노력하고 연구하는 과정에서 계속하고 있음을 말해 입니다 문제.

이 문제에 대한 도움을 주시면 감사하겠습니다. 감사합니다 !!!

+0

서식 참고 : 여기

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() 

이 출력됩니다. 감사! –

+0

클래스가'object'에서 상속되도록하십시오. 그렇지 않으면 오래된 스타일의 클래스를 사용하고 있으며 이는 나쁘다. – Daenyth

+0

주목할만한 또 다른 사항 :이 함수는 classmethod가 아닌 인스턴스 메소드입니다. – Daenyth

답변

5

당신은 당신의 정의 클래스 메소드는 파이썬이라고

에 자기 누락이 오브젝트 당신은 메소드 이름에 자신을 넣어 깜빡 첫 번째 함수 인수

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

좋습니다! 지금 사용해 보겠습니다. 당신의 도움을 주셔서 감사합니다!!! –

+0

그 트릭을 했어 !! 감사합니다. –

+2

@ 마이클 : 답변을 수락해야합니다 (옆에있는 체크 표시) – Daenyth

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 

파이썬에서 자체 변수의 전달은 명시 적이어야합니다 (C++처럼 암시 적이 지 않음).

4

self을 클래스 메서드의 정의에 추가하는 것을 잊었습니다. 첫 번째 매개 변수 (관례에 따라 self이라고 함)는 개체 인스턴스에 대한 참조를 포함하며 명시 적으로 정의에 작성해야하지만 메서드가 호출 될 때 암시 적으로 추가됩니다.

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

이 더 잘 작동합니다. 즉, 인스턴스 메서드가 아닌 클래스 방법입니다 -

1

함수 정의는 참고로 자기`

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

없습니다. 파이썬에는 세 가지 메소드 유형이 있습니다.

  • 클래스 방법 첫 번째 인수로서 class 객체를 수신하는 방법
  • 정적 메소드인스턴스 메소드이 방법
  • 전혀 컨텍스트 방법입니다 첫 번째 인수로 class의 인스턴스를받습니다.

각 메소드 유형에는 다른 용도와 목적. 다른 사람들이 말했듯이 인스턴스 메서드에 self 인스턴스 인수가 누락되었습니다. 다음은 각각의 메소드 유형에 대한 간단한 예와 비교를 위해 호출되는 방법입니다. 4 공백으로 들여 쓰기 코드 조각을 기쁘게과 같은 :

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'] 
관련 문제