如何从另一个模块中的类返回函数?

2024-09-28 17:23:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我想为Zed Shaw的LPTHW制作我自己的RPG角色生成器。作业的一部分是为程序的每个“房间”创建一个新类,比如WelcomeScreen或{}。在

这是主程序。在

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文件,它试图从中提取内容。在

^{pr2}$

但是当我用python运行主程序时,它只是注销而不是从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$ 

如果我的问题标题不是我想问的,我会提前道歉,作为一个新手,有时不知道该问什么。在


Tags: nameselfdefprogramstartpronextroom
1条回答
网友
1楼 · 发布于 2024-09-28 17:23:57

返回的是字符串,而不是函数(或函数结果)。你可能想要这样的东西:

def hello_user(self):
    return self.get_name

或者

^{pr2}$

根据你的计划,我想你可能想要第二个。区别在于第一个返回get_name函数,而第二个返回get_name函数的结果。在

相关问题 更多 >