Skip to content
Advertisement

module as no attribute – self.variable.function(self) [closed]

I have a module call pcb_files.py that only have some imports like this -> import read

Then I have another module call Easy.py, that have a class (Mainwindow) and a method/funtion (function_pcb1)

Class MainWindow(xxx,xxx)
    .....
    .....
    def func_pcb1(self):
        pcb_files.read.main(self)

Right now everytime I press a pushbutton in my app I run the funtion “main” that is inside “read”. So far so good

What I want:

def func_pcb1(self):
    script=self.nome_do_script
    pcb_files.script.main(self)

Like you see in, now I have this : script=self.nome_do_script where “script” is a string type.

And now I just want to change one thing, in the place of “read” I want to put the “script” like i do in the image but it gives me an error -> AttributeError: module ‘pcb_files’ has no attribute ‘script’

Resuming, instead of call whats inside “script” variable, it’s calling the name script itself.

Now you’re asking why do you want that ? -> Answer: I want to call, and show to the user, in my app different files that will do different things

Advertisement

Answer

something.other is what Python calls attribute access, where "other" is the attribute name. If you want to access an attribute with a dynamic name, you can use getattr.

def func_pcb1(self):
    script = self.nome_do_script
    getattr(pcb_files, script).main(self)

In the long term, you’ll want to learn how to use dictionaries for these kinds of things.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement