[Python] Python class attribute using an argument [closed]

Discussão em 'Python' iniciado por Stack, Março 26, 2025.

  1. Stack

    Stack Membro Participativo

    I want to define a class attribute - an attribute that is shared across all class objects (defined outside __init__()) - but I need to use a variable that is created in another module for defining it. How can I do that?

    For example, I have two files, the file my_class.py where I define the class in which I want to attach a class attribute, and the main.py file with a configuration object with let's say a path.

    main.py file:

    d = {'a_path': 'path/to/a/json/file.json'}


    my_class.py file:

    import json

    class MyClass:
    # I want to define here a class attribute, but in
    # order to define it, I need to use the variable from
    # main.py Something like the following:
    with open(d['a_path']) as file:
    var = json.load(file)

    def __init__(self):
    pass


    I want to create an object of MyClass to main.py file.

    Edited based on comments.

    Continue reading...

Compartilhe esta Página