Robin Lobel
Robin Lobel

Reputation: 780

Inherit from a class not imported yet?

I have a modules.py file defining several base classes.

One of these base classe inherit itself from a class defined in another module:

class MyClass(torch.nn.Module):
    ...

However, I'd prefer not to import torch in that modules.py file yet, because it's quite a big library, and this modules.py file is used by multiple processes, which would mean all of these processes would also have to import torch, leading quickly to an overflow (been there, done that).

Is there a way to define MyClass and specify it's based on torch.nn.Module without having to import torch just yet, and let the processes who really need the torch module do the import themselves ? So the other processes not needing MyClass could just ignore it's there and not try to resolve it, for instance ?

Upvotes: 0

Views: 359

Answers (1)

Serge Ballesta
Serge Ballesta

Reputation: 148890

There is a rather advanced way to simulate dynamic import by building a dummy class and importing the real module and overwriting its attributes on first call of its __new__ special method.

Demo:

module A.py:

class Attempt:
    _changed = False
    def __new__(cls, i):
        if not cls._changed:
            cls._changed = True
            import B
            for name, member in B.B.__dict__.items():
                if name not in ('__dict__', '__module__'):
                    setattr(cls, name, member)
        return object.__new__(cls)

module B.py:

print('importing module B')

class B:
    a = 5
    def __init__(self, i):
        self.i = i
    def __repr__(self):
        return f'B({self.i})'

You can then use it that way:

>>> import A
>>> a = A.Attempt(2)
importing module B
>>> b = A.Attempt(3)
>>> print(a,b)
B(2) B(3)

Which proves that the module B is only loaded at creation of the first object.

Upvotes: 1

Related Questions