[Python] Getting attribute from other file using a variable?

Discussão em 'Python' iniciado por Stack, Maio 16, 2025.

  1. Stack

    Stack Membro Participativo

    How do I put a variable into "getattr" to call an attribute from another file (in python 3)? The only thing I can think of gives a syntax error:

    File1:

    class Test:
    def __init__(self, test1, test2):
    self.test1 = test1
    self.test2 = test2

    a = Test("test", "test")


    File2:

    import File1

    def something(name):
    b = getattr(File2.{name}, test1)


    It needs to be in a function for the purpose I am using it for, but the curly braces gives syntax error. Is there any way that something like this would work?

    Continue reading...

Compartilhe esta Página