当前位置: 首页 > 面试题库 >

从另一个文件中的类导入函数?

乐刚毅
2023-03-14
问题内容

我在编写Python程序很有趣,但在尝试从另一个文件中的类导入函数时遇到问题。这是我的代码:

#jurassic park mainframe

from random import randint
from sys import exit
from comm_system import Comm_system #the file i want to import from



class Jpark_mainframe(object):
    def mainframe_home(self):
        print "=====Welcome to the Jurassic Park Mainframe====="
        print "==========Security Administration==============="
        print "===========Communications Systems==============="
        print "===============System Settings=================="
        print "===================Quit========================="

        prompt = raw_input("What would you like to do? ")

        while prompt != "Quit":

            if prompt == "Security Administration":
                print "Please enter the 5-digit passcode:"
                security_passcode = "%d%d%d%d%d" % (2, 0, 1, 2, randint(1, 2))
                security_guess = raw_input(": ")
                security_guesses = 0

                while security_guess != security_passcode and security_guesses < 7:
                    print "Incorrect. Please enter the security passcode."
                    security_guesses += 1
                    security_guess = raw_input(": ")

                    if security_guess == security_passcode:
                        print "=========Security Administration======="
                        print "Area 1 Fences: Off"
                        print "Area 2 Fences: On"
                        print "Area 3 Fences: Off"
                        print "Velociraptor Compound: Off"
                        print "Lobby Security System: Off"
                        print "Entrance Facility System: Off"
                        print "To enable all systems, enter 'On'"


                        enable_security = raw_input(": ")

                        if enable_security == "On":
                            print "Systems Online."


            if prompt == "System Settings":
                print "You do not have access to system settings."
                exit(0)


            if prompt == "Communications Systems":
                print "===========Communications Systems==========="
                print "error: 'comm_link' missing in directories"
                exit(0)
            return Comm_system.run #this is where I want to return the 
                                                   #the other file

the_game = jpark_mainframe()
the_game.mainframe_home()

我想返回一个run()从另一个文件中的类调用的函数。当我导入文件时,它首先运行其中的类run(),然后继续运行原始代码。为什么会这样?

这是comm_system的代码:

#communication systems


from sys import exit

class Comm_system(object):
def run(self):

    comm_directory = ["net_link", "tsfa_run", "j_link"]
    print "When the system rebooted, some files necessary for"
    print "communicating with the mainland got lost in the directory."
    print "The files were poorly labeled as a result of sloppy"
    print "programming on the staff's part. You must locate the"
    print "the file and contact the rescue team before the dinosaurs"
    print "surround the visitor's center. You were also notified the"
    print "generators were shorting out, and the mainframe will lose"
    print "power at any moment. Which directory will you search in?"
    print "you don't have much time! Option 1: cd /comm_sys/file"
    print "Option 2: cd /comm_sys/dis"
    print "Option 3: cd /comm_sys/comm"

    dir_choice = raw_input("jpark_edwin$ ")

    if dir_choice == "/comm_sys/file" or dir_choice == "/comm_sys/dis":
        print "misc.txt" 
        print "You couldn't locate the file!"
        print "The system lost power and your computer shut down on you!"
        print "You will not be able to reach the mainland until the system"
        print "comes back online, and it will be too late by then."
        return 'death'

    if dir_choice == "/comm_sys/comm":
        comm_directory.append("comm_link")
        print comm_directory
        print "You found the right file and activated it!"
        print "Just in time too, because the computers shut down on you."
        print "The phonelines are radios are still online."
        print "You and the other survivors quickly call the mainlane"
        print "and help is on the way. You all run to the roof and wait"
        print "until the helocopter picks you up. You win!"
a_game = Comm_system()
a_game.run()

问题答案:
from otherfile import TheClass
theclass = TheClass()
# if you want to return the output of run
return theclass.run()  
# if you want to return run itself to be used later
return theclass.run

将通讯系统的结尾更改为:

if __name__ == '__main__':
    a_game = Comm_system()
    a_game.run()

总是在运行的那些行会导致它在导入和执行时都运行。



 类似资料:
  • 问题内容: 在将其标记为重复之前, 请阅读我的问题: 我正在尝试从子目录的文件中导入类 并且在我的课程中有()我尝试了什么: 放入main.py: 我收到错误消息: 从文件导入Klasa ImportError:没有名为“文件”的模块 当我尝试使用时: 我收到此错误: tmp = Klasa() NameError:未定义名称“ Klasa” 我在子文件夹中放了一个空格,它仍然不起作用,而我在 :

  • 测试文件使用从另一个文件导入的函数 我如何能模拟返回值为这个函数在我的测试这个文件?我在开玩笑。

  • 问题内容: 如何将变量从一个文件导入到另一个文件? 示例:具有变量以及如何将其传递给? 如何将 所有 变量从一个导入到另一个? 问题答案: 将导入file1中的所有对象和方法

  • 如何将变量从一个文件导入到另一个文件? 示例:具有变量和如何将它们传递到? 如何将所有变量从一个导入到另一个?

  • 问题内容: 我是React的新手。希望使用单独文件中的少量组件来开发应用程序并将其导入到我的App.js中 我尝试过但无法弄清楚我在做什么错。 这是我的html: 这是我的App.js :( 来自js /目录) 这是我的MyComp.js (来自js / components /目录) 如果我这样尝试,我什么也看不到。而如果我在App.js中创建类,则它就像一个魅力。 有什么建议我在做什么错? 问

  • 我的项目目录如下所示: 在我的主要范围内。py,我从文档导入一个函数。py如下: 它工作正常。 如何在中导入相同的东西?我尝试了: 但我有一个错误: