알고리즘을 구현하기위한 클래스를 작성 중입니다. 이 알고리즘은 3 단계의 복잡성을 가지고 있습니다. 그것은이 같은 클래스를 구현하기 위해 나에게 의미가 있습니다 : 나는 calc_algorithm
각 클래스에서 재정의 될 것으로 기대파이썬에서 클래스 앨리어싱
class level0:
def calc_algorithm(self):
# level 0 algorithm
pass
# more level0 stuff
class level1(level0):
def calc_algorithm(self):
# level 1 algorithm
pass
# more level1 stuff
class level2(level1):
def calc_algorithm(self):
# level 2 algorithm
pass
# more level2 stuff
. 특정 명령 줄 옵션에 따라 데이터에서 수준 0, 수준 1 또는 수준 2를 실행하고 싶습니다. 이것은 내가 알고리즘을 호출하는 방법입니다
for offset in itertools.product(*map(xrange, (dim[0] - 1, dim[1] - 1, dim[2] - 1))):
algorithm(offset).calc_algorithm
algorithm
는 level0
, level1
또는 level2
중 하나입니다.
내가 다른 언어를 할 줄 방법은 다음과 같습니다 내가 할 수 있도록
for offset in itertools.product(*map(xrange, (dim[0] - 1, dim[1] - 1, dim[2] - 1))):
if (level == 0):
level0(offset).calc_algorithm
else:
if (level == 1):
level1(offset).calc_algorithm
else:
level2(offset).calc_algorithm
다른 클래스를 참조하는 클래스의 별명을하는 파이썬 방법이 있나요 :
algorithm = (level == 0) and level0 or (level == 1) and level1 or level2
을 입력 한 다음 위와 같이 algorithm
을 호출 하시겠습니까?
그냥 Aspect 지향적 인 언어가이 같은 클래스 쓸 수있다 Specman을,의 비교에 : 나는 level
구조체 멤버를 설정하면 내가 나머지를 사용할 수 있습니다, 다음
struct algorithm {
level: uint;
// common stuff for all levels
calc_algorithm() is empty;
when (level == 0) {
calc_algorithm() is only {
// level 0 algorithm
};
};
when (level == 1) {
calc_algorithm() is only {
// level 1 algorithm
};
};
when (level == 1) {
calc_algorithm() is only {
// level 1 algorithm
};
};
};
과 수업 중 투명하게
이 정말 클래스를 앨리어싱되지로 레벨을 통과 할 수 - 그것은 "더있어 명령 줄 인수를 기반으로 클래스 선택 "또는"사용자 입력을 기반으로 클래스 선택 "을 선택합니다. –
맞긴하지만 명령 행이나 사용자 입력에서 올 필요는 없습니다. 클래스 사이의 구별은 이전 계산의 결과 일 수 있습니다. –