[Python] What am I doing wrong with how I'm referencing my Python variables? [closed]

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

  1. Stack

    Stack Membro Participativo

    My code appears to be creating and referencing some variables with values of 0, despite me never setting those variables to 0. But it only references those variables as 0 when they exist in the context of me telling a piece of equipment to move, despite the equipment object itself not containing or referencing those variables.

    File structure:

    src
    |_variables_folder
    |_ __init__.py
    |_variables1_file.py
    |_equipment_folder
    |_ __init__.py
    |_equipment1_file.py
    |_modules_folder
    |_ __init__.py
    |_module1_file.py
    |_operations_folder
    |_ __init__.py
    |_operation1_file.py


    In variables1_file.py:

    x = 1
    y = 2
    z = 3

    equipment_object1 = None


    In equipment1_file.py:

    @dataclass
    class equipment1_class():
    _connection: object = None

    def __post_init__(self):
    self.connect_equipment1()

    def connect_equipment1(self):
    # Code which connects to a piece of equipment
    self._connection = # object produced by equipment-specific DLLs

    def move_equipment1(self, x, y, z):
    self._connection.move(x, y, z) # move() is a function of the object produced by the equipment-specific DLLs

    def find_position(self):
    return self._connection.find_position() # find_position() is a function of the object produced by the equipment-specific DLLs, and it returns the position of the equipment as [x, y, z]


    In module1_file.py:

    import variables_folder.variables1_file as variables1
    from equipment_folder.equipment1_file import *

    def connect_equipment1():
    variables1.equipment_object1 = connect_equipment_1()

    def set_equipment_initial_position():
    variables1.x, variables1.y, variables1.z = variables1.equipment_object1.find_position()

    def move_to_initital_position():
    variables1.equipment_object1.move(variables1.x, variables1.y, variables1.z)


    In operation1_file.py:

    # Note: We assume the equipment starts at position (x,y,z)=(10,20,30)

    import modules_folder.module1_file as module1

    if __name__ == "__main__":
    with module1.variables1.equipment_object1 as module1.variables1.stages:

    # print output 1
    print(module1.variables1.x, module1.variables1.y, module1.variables1.z)

    # print output 2
    print(module1.variables1.equipment_object1.find_position())

    module1.variables1.equipment_object1.set_equipment_initial_position()

    # print output 3
    print(module1.variables1.x, module1.variables1.y, module1.variables1.z)

    # print output 4
    print(module1.variables1.equipment_object1.find_position())

    module.variables.equipment_object1.move(100, 200, 300)

    # print output 5
    print(module1.variables1.x, module1.variables1.y, module1.variables1.z)

    # print output 6
    print(module1.variables1.equipment_object1.find_position())

    module.variables.equipment_object1.move(module1.variables1.x, module1.variables1.y, module1.variables1.z)

    # print output 7
    print(module1.variables1.x, module1.variables1.y, module1.variables1.z)

    # print output 8
    print(module1.variables1.equipment_object1.find_position())

    module.variables.equipment_object1.move(100, 200, 300)
    module.variables.move_to_initial_position()

    # print output 9
    print(module1.variables1.x, module1.variables1.y, module1.variables1.z)

    # print output 10
    print(module1.variables1.equipment_object1.find_position())


    What comes out when I run my operation1_file.py code is this (I have numbered the print outputs for convenience):

    1. 1 1 1 # As expected, variables1_file is being correctly called through module1_file
    2. 10 20 30 # As expected, the equipment position is being read correctly

    # Here I call module1.variables1.equipment_object1.set_equipment_initial_position(), which sets x, y, and z in variables1_file.py, as referenced by module1.variables1, to the current equipment position.

    3. 10 20 30 # As expected, x, y, and z in variables1_file.py, as referenced by module1.variables1, are being updated correctly
    4. 10 20 30 # As expected, the equipment position is being read correctly

    # Here I call module.variables.equipment_object1.move(100, 200, 300), which moves the equipment to (x,y,z)=(100,200,300)

    5. 10 20 30 # As expected, x, y, and z in variables1_file.py, as referenced by module1.variables1, are not altered by the movement command
    6. 100 200 300 # As expected, the equipment position is being read correctly

    # Here is where things start to go wrong. I call module.variables1.equipment_object1.move(module1.variables1.x, module1.variables1.y, module1.variables1.z) and the equipment moves to (x,y,z)=(0,0,0)

    7. 10 20 30 # As expected, x, y, and z in variables1_file.py, as referenced by module1.variables1, are not altered by the movement command
    8. 0 0 0 # ??? Somehow the equipment moved to (x,y,z)=(0,0,0) ???

    # I move the stage back to (x,y,z)=(100,200,300) and call module.variables1.move_to_initial_position() and again it moves to (x,y,z)=(0,0,0)

    9. 10 20 30 # As expected, x, y, and z in variables1_file.py, as referenced by module1.variables1, are not altered by the movement command
    10. 0 0 0 # ??? Somehow the equipment moved to (x,y,z)=(0,0,0) ???


    I have used print commands to output the x, y, and z values in variables1_file.py at every level of code I can conceive of. They always report exactly what they should. But whenever I use those variables to command a move, from any level of the code, they act as though they are 0. Even when immediately above and below the movement command I tell it to print the values and they're not 0, it moves to 0! Even when I have never set them to 0 at any level of the code!

    It can't be anything weird going on inside the movement command either, because the movement command is from a file which doesn't even reference the variables. I'm passing these variables into the movement command as arguments, not calling them from variables1_file.py from within the movement command. It's not the movement command itself either, it moves correctly when I give it constants to work with.

    It is specifically when I pass these variables into the movement command as arguments that everything goes haywire.

    The only thing I can imagine is something very weird going on with the with I'm using. I hope that's not the case though, because this equipment absolutely needs to be called from a with, so it uses __exit__() to crash out appropriately if the program gets terminated.

    Continue reading...

Compartilhe esta Página