Trying to select objects by name with script, don't understand this behavior I want to run this script and have it pick up references the four pre-existing "my_xxx" objects, so that the script can then operate on them. However, all four variables end up referring to my_Sun, and it is the only object that moves.

Question: I would like to understand why this isn't working the way I expect, am I missing something obvious?

Question: What is the correct way to assign python variables to pre-existing objects based on name, if this isn't right?

import bpy

bpy.ops.object.select_all(action="DESELECT")

bpy.ops.object.select_pattern(pattern="my_Cube")
cube = bpy.context.active_object
bpy.ops.object.select_all(action="DESELECT")

bpy.ops.object.select_pattern(pattern="my_Cone")
cone = bpy.context.active_object
bpy.ops.object.select_all(action="DESELECT")

bpy.ops.object.select_pattern(pattern="my_Suzanne")
monkey = bpy.context.active_object
bpy.ops.object.select_all(action="DESELECT")

bpy.ops.object.select_pattern(pattern="my_Sun")
sun = bpy.context.active_object
bpy.ops.object.select_all(action="DESELECT")

print ("monkey.name: ", monkey.name)
print ("cone.name :  ", cone.name)
print ("cube.name:   ", cube.name)
print ("sun.name:    ", sun.name)

monkey.location = 5,5,5  # note to self: use "location" not "position" !!!
cone.location   = 5,5,5
cube.location   = 5,5,5
sun.location    = 5,5,5


Output to terminal:

monkey.name:  my_Sun
cone.name :   my_Sun
cube.name:    my_Sun
sun.name:     my_Sun


RESULTS: Only sun is moved to new location 5, 5, 5
The script is setting the location of whatever object is active when the script is run.

bpy.ops.object.select_pattern(pattern="my_Cube")


assigns to the  context selected objects list.  This is the list of all objects in the scene whose select property is true (obj.select).  Using "my_" as a search string for the op would select all your "my_" objects.  The operator setting one as active is somewhat meaningless. Use

monkey = bpy.context.selected_objects[0]


to assign the first of the selected objects list,  after running that operator.  

The context active object,  is best set using

scene.objects.active = obj


after running an operator that sets active object.

Suggest replacing all the annoying operators  with API calls something akin to:

import bpy

context = bpy.context
scene = context.scene

for name in ["Cube", "Cone", "Lamp", "Suzanne"]: # not a my_ fan 8^)
    obj = scene.objects.get(name)
    if obj:
        obj.location = (5, 5, 5)