2014-04-09 3 views
0
여기

은 (point.py에서) 클래스입니다 :다른 파일의 다른 클래스에있는 메서드를 호출하려면 어떻게해야합니까?

class Point: 
    def __init__(self, x, y): 
     self.x = x 
     self.y = y 

    def adjacent(self, pt1, pt2): 
     return ((pt1.x == pt2.x and abs(pt1.y - pt2.y) == 1) or 
     (pt1.y == pt2.y and abs(pt1.x - pt2.x) == 1)) 

    def distance_sq(self, p1, p2): 
     return (p1.x - p2.x)**2 + (p1.y - p2.y)**2 

내가 다른 파일에이 기능 (actions.py)가 있다고 가정 :이 기능이 얼마나

import point 

def find_nearest(world, pt, type): 
    oftype = [(e, distance_sq(pt, entities.get_position(e))) 
     for e in worldmodel.get_entities(world) if isinstance(e, type)] 

    return nearest_entity(of type) 

공지 사항

AttributeError: 'module' object has no attribute 'distance_sq' 

나는 differe에서 클래스의 메소드를 호출하기위한 올바른 구문을 기억할 수 :이 코드를 실행하려고하면 point.py에서 distance_sq 전화, 그것은 불평 NT 파일! 어떤 도움을 주셔서 감사합니다! 감사.

+2

'(x, y)를 .distance_sq()'또는'point.Point A = (X, Y)'다음 '을 a.distance_sq()' – skamsie

+0

흠 지금 그것은 불평하고있다 : NameE rror : 글로벌 이름 'x'가 정의되지 않았습니다. (a = point.Point (x, y) 옵션을 사용했습니다.) – Karen

+0

x, y는 인스턴스 변수입니다. 문자열, 정수 또는 실제 의도 한대로 실제 값으로 대체해야합니다. – skamsie

답변

-1

먼저 해당 클래스의 인스턴스를 만들지 않고 클래스의 멤버 메서드를 직접 호출 할 수 없습니다.

import point 
point.distance_sq(point1, point2) 

또는, 좋은 방법이됩니다

class Point: 
    def __init__(self, x, y): 
     self.x = x 
     self.y = y 

    def adjacent(self, pt1, pt2): 
     return ((pt1.x == pt2.x and abs(pt1.y - pt2.y) == 1) or 
     (pt1.y == pt2.y and abs(pt1.x - pt2.x) == 1)) 

def distance_sq(p1, p2): 
    return (p1.x - p2.x)**2 + (p1.y - p2.y)**2 

는 그런 다음 같은이 함수를 호출 할 수 있습니다 point.py에서

: 그것은처럼 distance_sq 방법은 외부 클래스 선언을해야처럼 보인다 다음과 같은 클래스 메소드를 만들 수 있습니다 :

In point.py :

class Point: 
    def __init__(self, x, y): 
     self.x = x 
     self.y = y 

    def adjacent(self, pt1, pt2): 
     return ((pt1.x == pt2.x and abs(pt1.y - pt2.y) == 1) or 
     (pt1.y == pt2.y and abs(pt1.x - pt2.x) == 1)) 

    @classmethod 
    def distance_sq(cls, p1, p2): 
     return (p1.x - p2.x)**2 + (p1.y - p2.y)**2 

다음에, 원하는 전화 :

import point 
point.Point.distance_sq(point1, point2) 
+0

downvote가 어디서 왔는지 궁금합니다 ... – giga

0

당신의 Point 클래스의 경우, 정의 된 방법 중 어느 것도 모두에서 인스턴스 (self)을 참조하십시오. 당신은 point 모듈에서 다음 중 하나의 기능을해야한다, 또는 당신이 선호하는 경우에 그 정적 메서드하게, 그 클래스의 네임 스페이스 유지 : 당신이 staticmethod 접근 방식을 사용하는 경우

class Point: 
    def __init__(self, x, y): 
     self.x = x 
     self.y = y 

    @staticmethod 
    def adjacent(pt1, pt2): 
     return ((pt1.x == pt2.x and abs(pt1.y - pt2.y) == 1) or 
     (pt1.y == pt2.y and abs(pt1.x - pt2.x) == 1)) 

    @staticmethod 
    def distance_sq(p1, p2): 
     return (p1.x - p2.x)**2 + (p1.y - p2.y)**2 

그런 다음 point에서 Point 클래스를 가져옵니다

from point import Point 

... Point.distance_sq(pt, entities.get_position(e)) 

또는 point을 가져오고 대신에 기능을 사용하는 경우 point.distance_sq을 사용하십시오. 그런 다음

def adjacent(self, point): 
    return (
     (self.x == point.x and abs(self.y - point.y) == 1) or 
     (self.y == point.y and abs(self.x - point.x) == 1) 
    ) 

def distance_sq(self, point): 
    return (self.x - point.x)**2 + (self.y - point.y)**2 

당신이에 point를 가져올 필요가 없습니다 ptentities.get_position(e) 모두 Point의 인스턴스를 경우

아마도 더 나은 방법은 항상 현재 인스턴스가 두 가지 방법에 pt1을하는 것입니다 모든 단 수행

pt.distance_sq(entities.get_position(e)) 
point.Point
관련 문제