简体   繁体   中英

python call method from external function of class

I'm working on a function where an application is already created but I can't edit

class mainProject():
     def __init__(self, screen):
          module1 = importlib.import_module(extension)
          module1.openBox()


     def boxIsOpen(self):
          .......

I try to execute boxIsOpen() into my custom function openBox() in the extension module.

It is possible to do that? I try to reinitialized the class into the function but it is not the previous function. The "caller" keyword don't really works.

Thanks

Generally speaking, importing in the middle of code, let alone in a constructor isn't advisable.

import extension
class mainProject():
    def __init__(self, screen):
        extension.openBox()

    def boxIsOpen(self):
        ...

Should do the trick.

If you cannot touch the original otherwise, you should consider subclassing mainProject instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM