best way to call instance variable from other places/directory?

Viewed 33

In my project I have a settings.py which contains a class and a constructor. (It contains more than 70 instance variables.)

I know I can call that variable with the help of by creating class object.value, but I have more than 25 folders. Is calling with help of a class name the best approach? Will it slow performance?

My directory structure:.

├───A
│   ├───.idea
│   │   └───inspectionProfiles
│   ├───abrest
│   │   └───app
│   │       ├───config
│   │       │   └───settings.py
                └───log_config.py
│   │       ├───handle
│   │       └───utils
│   ├───utils
│         └───test.py

a) setting.py

class BaseConfig:
    def __init__(self):
        self.A = '/api'
        self.B = False
        self.C = False
        self.D = "0.0.0.0"
        self.E = "localhost"
        self.F = 8000
        self.G = "test"
        self.H = 'tokens'
        self.I = 'default-secret'

b) How can I call inside other directory without doing class name (because I believe it is not the best practice if there are more than 100 variables).

For example, I am calling variable in:

test.py

from abrest.app.config.settings import BaseConfig

print(BaseConfig().B)

Output:

PycharmProjects/A/utils/test.py False Is there any way to call it from config.variable_name OR config.log_config_variable? (And also I need suggestions on how it will affect performance.)

1 Answers

If all the attributes are constant for all the instances, its better to create those as class variables instead of instance variables. For example I would write the setting class as :

class Config:
  NAME = 'settings'
  DEBUG = True
  BASE_DIR = '~/config/'

and access it as Config.NAME etc, so that I don't have to create an instance. Otherwise you can use the attributes directly by importing like this,

from Config import *
Related