Python 导入 class 以在另一个 class 中用作 nested/inner class

Python import class to use as nested/inner class in another class

Python版本:

Python 3.6

我有这个class:

class Backup:
    class Create:
        @staticmethod
        def all():
            print('create all')

        @staticmethod
        def user(username):
            print('create user: ' + username)

        @staticmethod
        def file(filename):
            print('create file: ' + filename)

    class Restore:
        @staticmethod
        def all():
            print('restore all')

        @staticmethod
        def user(username):
            print('restore user: ' + username)

        @staticmethod
        def file(filename):
            print('restore file: ' + filename)

    class Delete:
        @staticmethod
        def all():
            print('delete all')

        @staticmethod
        def user(username):
            print('delete user: ' + username)

        @staticmethod
        def file(filename):
            print('delete file: ' + filename)

但我想将嵌套的 classes 移动到它们自己的模块中并导入它们,但我不知道该怎么做。这是我基于一些类似的堆栈溢出问题的最新尝试。显然它不起作用:

from Models.Create import Create

class Backup(Create):

有谁知道如何实现我正在尝试做的事情,所以我仍然可以像这样调用 classes 中的方法:Backup.Create.all()

一种方法是在外部导入内部 classes。

class Backup:
    from Models.Create import Create
    from Models.Delete import Delete
    from Models.Restore import Restore

如果你这样做,我会将它们全部导入 'Models' 包的 __init__.py,所以你可以这样做:

class Backup:
    from Models import Create, Delete, Restore

如果您要将它们导入其他地方,另一种选择是在 class.

中分配它们
class Backup:
    Create = Models.Create.Create
    Delete = Models.Delete.Delete
    Restore = Models.Restore.Restore