Python: Create List Of Object References
Solution 1:
Every Python list
always is internally an array of references (in CPython, which is no doubt what you're using, at the C level it's an array of PyObject*
-- "pointers to Python objects").
No copies of the objects get made implicitly: rather (again, in CPython) each object's reference count gets incremented when the you add "the object" (actually a reference to it) to the list. In fact when you do want an object's copy you need to specifically ask for one (with the copy
module in general, or sometimes with type-specific copy methods).
Multiple references to the same object are internally pointers to exactly the same memory. If an object is mutable, then mutating it gets reflected through all the references to it. Of course, there are immutable objects (strings, numbers, tuples, ...) to which such mutation cannot apply.
So when you do, e.g,
sample_list = [select_one_name, select_two_name, etc]
each of the names (as long as it's in scope) still refers to exactly the same object as the corresponding item in sample_list
.
In other words, using sample_list[0]
and select_one_name
is totally equivalent as long as both references to the same object exist.
IOW squared, your stated purpose is already accomplished by Python's most fundamental semantics. Now, please edit the Q to clarify which behavior you're observing that seems to contradict this, versus which behavior you think you should be observing (and desire), and we may be able to help further -- because to this point all the above observations amount to "you're getting exactly the semantics you ask for" so "steady as she goes" is all I can practically suggest!-)
Added (better here in the answer than just below in comments:-): note the focus on mutating operation. The OP tried test[0]= somelist
followed by test[0].append
and saw somelist
mutated accordingly; then tried test[0] = [1, 2]
and was surprised to see somelist
not changed. But that's because assignment to a reference is not a mutating operation on the object that said reference used to indicate! It just re-seats
the reference, decrement the previously-referred-to object's reference count, and that's it.
If you want to mutate an existing object (which needs to be a mutable one in the first place, but, a list
satisfies that), you need to perform mutating operations on it (through whatever reference, doesn't matter). For example, besides append
and many other named methods, one mutating operation on a list is assignment to a slice, including the whole-list slice denoted as [:]
. So, test[0][:] = [1,2]
would in fact mutate somelist
-- very different from test[0] = [1,2]
which assigns to a reference, not to a slice.
Solution 2:
This is not recommended, but it works.
sample_list = ["select_one_name", "select_two_name", "select_three_name"]
for select in sample_list:
locals()[select] = Select(
title = 'test',value = 'first_value',
options = ['first_value', 'second_value', 'etc']
)
You can use select_one_name, select_two_name, etc directly because they're set in the local scope due the special locals() list. A cleaner approach is to use a dictionary, e.g.
selects = {
'select_one_name': Select(...),
'select_two_name': Select(...),
'select_three_name': Select(...)
}
And reference selects['select_one_name']
in your code and you can iterate over selects.keys()
or selects.items()
.
Post a Comment for "Python: Create List Of Object References"