ARGH once again Python& #39;s lack of methods (that I can find) for object indirection bites me on the butt. Sigh.
(I& #39;ll explain what I mean but it& #39;s nerdy and only three people here should bother reading it)
(I& #39;ll explain what I mean but it& #39;s nerdy and only three people here should bother reading it)
Three objects of the same class: friend1, friend2, friend3. If I need to do indirection for data WITHIN the object, the data dictionary is my friend. By indirection I mean using a string to provide the name of the data item, for variability:
cur_rel = vars(friend1)["rel_" + current_human]
instead of
if current_human == elizabeth:
cur_rel = friend1.rel_elizabeth
if current_human == ben:
cur_rel = friend1.rel_ben
...
which saves me a lot of code. BUT! This won& #39;t work with a string for the OBJECT.
instead of
if current_human == elizabeth:
cur_rel = friend1.rel_elizabeth
if current_human == ben:
cur_rel = friend1.rel_ben
...
which saves me a lot of code. BUT! This won& #39;t work with a string for the OBJECT.
so I can& #39;t do
whichfriend = "friend" + str(friend_ordinal)
cur_rel = vars(whichfriend)["rel_" + current_human]
The argument to vars must be 1) an object handle 2) special handle "self" within object code 3) empty, for scope of all local
And NOTHING else is allowed.
whichfriend = "friend" + str(friend_ordinal)
cur_rel = vars(whichfriend)["rel_" + current_human]
The argument to vars must be 1) an object handle 2) special handle "self" within object code 3) empty, for scope of all local
And NOTHING else is allowed.
So I have to do
if friend_ordinal == 1:
cur_rel = vars(friend1)["rel_" + current_human]
if friend_ordinal == 2:
cur_rel = vars(friend2)["rel_" + current_human]
...etc
which results in many lines of nearly-redundant code
if friend_ordinal == 1:
cur_rel = vars(friend1)["rel_" + current_human]
if friend_ordinal == 2:
cur_rel = vars(friend2)["rel_" + current_human]
...etc
which results in many lines of nearly-redundant code