I'd like to be able to refer to the same object multiple times from python. I'd ideally like to be able to move back and forth between using the python console and using the UI.
Its better practice not to reference objects by names at all, once created you can store the data in a list, dictionary, on a class etc, there is rarely a reason to have to keep searching for the same data by name.
This suggests keeping a reference to an object directly, rather than by name.
Further down the same page, under the heading "Help! My script crashes Blender", we have the following:
Do not keep direct references to Blender data (of any kind) when modifying the container of that data, and/or when some undo/redo may happen (e.g. during modal operators execution…). Instead, use indices (or other data always stored by value in Python, like string keys…), that allow you to get access to the desired data.
And a little further down:
As suggested above, simply not holding references to data when Blender is used interactively by the user is the only way to ensure the script doesn’t become unstable.
This suggests that keeping a reference to an object directly is unsafe if we'd like to work interactively, and we should instead refer to an object by name (I think this is what is meant by "string key") or by index. If I've understood correctly, the index of an object may change if other objects are removed, so accessing objects by index is also unreliable.
Is there a safe, reliable way to maintain a reference to an object, or is this just a pain point of the python API?