1

Python version:

Python 3.6

I have this 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)

But I want to move the nested classes to their own modules and import them, but I am not sure how to do that. Here was my latest attempt based on some similar stack overflow questions. Obviously it didn't work:

from Models.Create import Create

class Backup(Create):

Does anyone know how to achieve what I am trying to do so I can still call the methods in the classes like this: Backup.Create.all()

Rob
  • 6,758
  • 4
  • 46
  • 51
  • Check the `super()` functions : https://stackoverflow.com/questions/576169/understanding-python-super-with-init-methods – alkasm Dec 13 '17 at 05:43
  • Despite appearances, I don't think he's looking to extend any functions. He's trying to nest them. – Brett Beatty Dec 13 '17 at 05:47

1 Answers1

3

One way to do it would be to import the inner classes within the outer.

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

If you do it this way, I would import them all into the 'Models' package's __init__.py, so you could just do this:

class Backup:
    from Models import Create, Delete, Restore

Another option if you're importing them elsewhere is just assigning them within the class.

class Backup:
    Create = Models.Create.Create
    Delete = Models.Delete.Delete
    Restore = Models.Restore.Restore
Brett Beatty
  • 5,690
  • 1
  • 23
  • 37
  • What do you mean import them into the __init__.py? Do you have an example? – Rob Dec 13 '17 at 06:01
  • I'm assuming your Models directory has an \__init__.py for the Models package to work. If in that file you import classes from that package's modules (such as Create, Delete, Restore), you can reference them from the package and not the module. As far as examples go, you could look at requests (https://github.com/requests/requests/blob/master/requests/__init__.py#L116). When creating a requests session, one imports requests.Session instead of requests.sessions.Session. – Brett Beatty Dec 14 '17 at 07:01
  • mypy gives `Unsupported class scoped import` message for this approach is it normal? – alper Jul 25 '22 at 15:09