pythonclassobjectbuilt-in-types

Python getting values from class


im trying to make an app that runs few files to make it organized, however im kinda lurker in python and got stuck trying to import values from settings.py in main.py.

The error type im getting looks like this depending on how im trying to get the object's value:

self.code = sets.run().self.code
AttributeError: 'NoneType' object has no attribute 'self'

or

self.code = sets.code
AttributeError: 'settings' object has no attribute 'code'

The whole idea is to make get self. values in the other file not using the return but simply specifying the element.

Simple sample code:

Main file:

import settings
def run():
    sets = settings.settings()
    code = sets.code 
run()

Settings file:

class settings():
    def run(self):
       self.code = somevalue

so basicaly that is the minimum extension of what im trying to do, i could return the self.code in run however that is not what i want because i have many settings_values and id like to access them in different files. help much appreciated.

Edit: So how should i approach this if i need to import settings in main but only run() settings whenever i start running the main_run() and than just get the variables? I dont want to complicate it with saving to file and than reading a list() i need to just access them and only run

basicaly: my settings gets a long list from pickle and than makes some operations that return the code combination and lots of other self.settings that need to be initiated when the program(main) starts and than for the whole While True inside run() the values are fixed unless i start refresh_settings() and change the settings than the data is recompiled and the program returns to while true


Solution

  • You need and __init__ function, your class should look like this:

    class settings():
    def __init__(self,code):
        self.code = code
    

    Now you can make a variable like this

    sets = settings(“something”)
    

    And Access the code in the variable in this way:

    code = sets.code
    

    Or if you really want to do the run() bit then your code would be:

    class settings():
    def __init__(self,code):
        self.code = code
    def run(self):
        return self.code