2012-12-14 3 views
0

제드 쇼 (Zed Shaw)의 LPTHW에서 예 45에 대한 내 자신의 RPG 캐릭터 생성기를 만들려고합니다. 과제의 일부는 WelcomeScreen 또는 ChooseMutations과 같이 프로그램의 각 '방'에 대해 새로운 수업을 만드는 것입니다.LPTHW 예. 45, 다른 모듈의 클래스에서 함수를 반환하는 방법?

다음은 주요 프로그램입니다. 여기

import rooms 

class Program(object): 

    def __init__(self, start): 
     self.start = start 

    def run(self): 
     next_room_name = self.start 

     while True: 
      room = getattr(self, next_room_name) 
      next_room_name = room() 

x = rooms.WelcomeRoom() 

Program(x.hello_user()) 

그리고

은 그것에서 물건을 끌어하려고 rooms 파일입니다. 내가 파이썬의 주요 프로그램을 실행할 때

class WelcomeRoom(object): 

    def __init__(self): 
     pass 

    def hello_user(self): 
     print '*' * 79 
     print '\n' 
     print '\t\tWelcome to the' 
     print '\t\tMetamorphosis Alpha Character & Random Encounter Generator' 
     print '\t\tProgrammed poorly by Raymond Weiss' 
     print '\n' 
     print '*' * 79 
     raw_input('Please press enter to continue') 
     return 'get_name' 

    def get_name(self): 

     name = raw_input('Hello, whats your name?', 
       '\n', 
       ':> ') 

는하지만, 그냥 rooms에서 기능 get_name()를 반환하는 대신 로그 아웃. 출력은 아래에 게시되었습니다. 내 질문 제목은 가끔 하드 정확하게 물어 무엇을 모르는 초보자로 물어하려고 무엇 메신저 정확하지 않은 경우

Raymond-Weisss-MacBook-Pro:macgre Raylug$ python macgre.py 
******************************************************************************* 


     Welcome to the 
     Metamorphosis Alpha Character & Random Encounter Generator 
     Programmed poorly by Raymond Weiss 


******************************************************************************* 
Please press enter to continue 
Raymond-Weisss-MacBook-Pro:macgre Raylug$ 

내가 미리 사과드립니다.

답변

1

함수 (또는 함수 결과)가 아닌 문자열을 반환합니다.

def hello_user(self): 
    return self.get_name 

또는 프로그램을 바탕으로

def hello_user(self): 
    return self.get_name() 

, 당신이 아마 두 번째를 원한다고 생각 : 당신은 아마 뭔가를 원한다. 차이점은 첫 번째 것은 get_name 기능을 반환하는 반면 두 번째 것은 get_name 기능을 반환한다는 것입니다.

+0

이것은 효과가 있습니다. 고맙습니다! 나는 그것이 가능한 한 빨리 대답으로 받아 들일 것이다. – lerugray