은 (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 파일! 어떤 도움을 주셔서 감사합니다! 감사.
'(x, y)를 .distance_sq()'또는'point.Point A = (X, Y)'다음 '을 a.distance_sq()' – skamsie
흠 지금 그것은 불평하고있다 : NameE rror : 글로벌 이름 'x'가 정의되지 않았습니다. (a = point.Point (x, y) 옵션을 사용했습니다.) – Karen
x, y는 인스턴스 변수입니다. 문자열, 정수 또는 실제 의도 한대로 실제 값으로 대체해야합니다. – skamsie