Ariel Anasco
Ariel Anasco

Reputation: 63

How to use a python module on inherited class

I have a base class A in base.py:

import module1

class A:
  def test(self):
     module1.sample("test")

Then in new.py I created a new class B which inherits A and override test method:

from base import A
class B(A):
    def test(self):
        module1.sample("test")
        print("Testing...")

The problem is that the module1 is no longer available in new.py. Is there any options that I do not need to import module1 again in new.py?

Upvotes: 0

Views: 119

Answers (1)

ghchoi
ghchoi

Reputation: 5156

One not recommended way to achieve what you want is to use __builtins__. Add the following line to base.py.

__builtins__['module1'] = module1

Then module1 is no longer undefined from new.py. It is definitely defined in __builtins__.

Again, it is not recommended, however, good to understand how Python works. You would better import module1 from new.py as well.

import module1

from base import A
...

Upvotes: 1

Related Questions