如何从外部文件将函数导入类?

问题描述 投票:0回答:1

我正在编写一个简单的井字游戏。我检查获胜的功能过于重复和庞大,因此我想将其放入外部文件中。我的想法是:

class Game(object):
    def __init__(self):
        pass
    import funcFile

instance = Game()
instance.func()

在funcFile.py中是:

def func():
    print("Hello world!")

但是:

Traceback (most recent call last):
    instance.func()
TypeError: 'module' object is not callable

有没有办法做到这一点,还是应该将所有内容都放在一个文件中?

python python-3.x function import pep8
1个回答
0
投票

您应该尝试from funcFile import func

© www.soinside.com 2019 - 2024. All rights reserved.