Newbie/novice Python guy here. I'm trying to avoid using global variables, and in some of my searches I was pointed in the direction of Class variables. But I'm struggling with scope and persistence. I simplified the code segment below, but its basically what I'm trying to do.
I have a sensor I need to run a long average on to handle the dither (variation). I want to keep the list/array of measures within the class and/or def and retain them between subsequent calls to the method/function. However when I run sensor.setup() I get an error "offset" is not defined. I assumed it would have been created with the instance and accessable by it's member setup().
Is there a link to some good examples? Or if there is a better way I'm all ears!
I have a sensor I need to run a long average on to handle the dither (variation). I want to keep the list/array of measures within the class and/or def and retain them between subsequent calls to the method/function. However when I run sensor.setup() I get an error "offset" is not defined. I assumed it would have been created with the instance and accessable by it's member setup().
Is there a link to some good examples? Or if there is a better way I'm all ears!
Code:
class myClass: range_mm = 0.0 average = 0.0 offset = 0 def setup(): i2c = busio.I2C(board.SCL, board.SDA) sensor = adafruit_vl6180x.VL6180X(i2c, offset) return sensor def read(): # read sensor return range_mm def read_algo(): read() # stats and magic here average = Xdef main(): sensor = myClass() # create instance of the class sensor.setup() if __name__ == "__main__": main()Statistics: Posted by OldPCGuy — Sun Oct 26, 2025 8:01 pm