I have a class that looks something like this:
class Foo:
import_extra = false
def __init__(import_extra):
if import_extra:
self.import_extra = true
import extra
def do_thing():
# do some stuff
if self.import_extra:
extra.do_extra_stuff()
This class has some functionality that depends on extra, but I want to be able to decide whether this functionality is available or not when I first initialize the class, which happens once at the start of running the application. Unfortunately, I can't seem to figure out how to import extra for the whole class so that the other method can use it.
I designed it this way because the extra module might not be available for import in some cases. do_thing gets called quite frequently, so I'm hoping that it's possible to avoid putting the import statement there.
Is there an easier way to do this that I'm missing?
extradoes or what it returns. What particular functionality is important for theclass Foo?NameError: extra is not definedwhen I runfoo = Foo(True); foo.do_thing(). The problem that I am trying to fix is that the import statement is in the scope of the__init__method, but I want the import to be usable for the whole class after initializing it.