Let's say we have a list g
with variable d
as its element:
Clear[g, d]
g = {1,2,3,d}
Now g[[4]]
evaluates to d
.
But after d
is assigned, g[[4]]
evaluates to d
's value.
But it seems like g[[4]]
is still pointing to d
:
In[48]:= d = 1
Out[48]= 1
In[49]:= g[[4]]
Out[49]= 1
In[50]:= d = 2
Out[50]= 2
In[51]:= g[[4]]
Out[51]= 2
So is there anyway to get the symbol of g[[4]]
? SymbolName
does not work in this case.
SymbolName[g[[4]]]
results in an error.
OwnValues[g]
andFullDefinition[g]
. The closest I can get isExtract[First[OwnValues[g]], {2, 4}, Defer]
but I cannot take theSymbolName
as it's wrapped inDefer
and removing that will evaluate it as 2. $\endgroup$StringCases[ToString@Extract[First[OwnValues[g]], {2, 4}, Defer], "Defer[" ~~ x_ ~~ "]" :> x] // First
$\endgroup$