Importing a function from a class in another file?

后端 未结 6 635
北恋
北恋 2020-12-05 03:55

I\'m writing a Python program for fun but got stuck trying to import a function from a class in another file. Here is my code:

#jurassic park mainframe

from         


        
相关标签:
6条回答
  • 2020-12-05 04:23

    It would really help if you'd include the code that's not working (from the 'other' file), but I suspect you could do what you want with a healthy dose of the 'eval' function.

    For example:

    def run():
        print "this does nothing"
    
    def chooser():
        return "run"
    
    def main():
        '''works just like:
        run()'''
        eval(chooser())()
    

    The chooser returns the name of the function to execute, eval then turns a string into actual code to be executed in-place, and the parentheses finish off the function call.

    0 讨论(0)
  • 2020-12-05 04:27

    You can use the below syntax -

    from FolderName.FileName import Classname
    
    0 讨论(0)
  • 2020-12-05 04:32
    from FOLDER_NAME import FILENAME
    from FILENAME import CLASS_NAME FUNCTION_NAME
    

    FILENAME is w/o the suffix

    0 讨论(0)
  • 2020-12-05 04:36

    First you need to make sure if both of your files are in the same working directory. Next, you can import the whole file. For example,

    import myClass
    

    or you can import the entire class and entire functions from the file. For example,

    from myClass import
    

    Finally, you need to create an instance of the class from the original file and call the instance objects.

    0 讨论(0)
  • 2020-12-05 04:39
    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
    

    Change the end of comm system to:

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

    It's those lines being always run that are causing it to be run when imported as well as when executed.

    0 讨论(0)
  • 2020-12-05 04:40

    If, like me, you want to make a function pack or something that people can download then it's very simple. Just write your function in a python file and save it as the name you want IN YOUR PYTHON DIRECTORY. Now, in your script where you want to use this, you type:

    from FILE NAME import FUNCTION NAME
    

    Note - the parts in capital letters are where you type the file name and function name.

    Now you just use your function however it was meant to be.

    Example:

    FUNCTION SCRIPT - saved at C:\Python27 as function_choose.py

    def choose(a):
      from random import randint
      b = randint(0, len(a) - 1)
      c = a[b]
      return(c)
    

    SCRIPT USING FUNCTION - saved wherever

    from function_choose import choose
    list_a = ["dog", "cat", "chicken"]
    print(choose(list_a))
    

    OUTPUT WILL BE DOG, CAT, OR CHICKEN

    Hoped this helped, now you can create function packs for download!

    --------------------------------This is for Python 2.7-------------------------------------

    0 讨论(0)
提交回复
热议问题