Can I get a value's name
yahoo at al-got-rhythm.net
Tue May 12 05:15:50 CEST 2009
Thank you Ken for your help answer
On Mon, May 11, 2009 at 8:11 PM, Ken Seehart <ken at seehart.com> wrote:
> jalanb3 wrote:
>> def replace_line(pattern,replacement):
>> values = '\n' in pattern and [ pattern ] or 
>> values += '\n' in replacement and [ replacement ] or 
>> Can I later get the name "pattern" via values?
> A value generally does not contain the
> "pattern" or any kind of name information (although class, method, and
> function instances contain some symbolic information in them).
That's why I took the question seriously at all:
It was an entirely different project - pulling info out of
tracebacks into logs.
And I can get names from a reference for classes, modules, ...
Why not variables ?
> Study the concept of 'references' and all of this will become more clear.
I shall dive into that koan :-)
> The only way to do something like what you want would be to search globals()
> or locals() for the value (depending on the context), but that would be an
> extremely ugly hack.
> Chances are that if you want to do this, you need to
> rethink the problem instead.
That does seem reasonable, but before I started writing Python it
would never occurred to me to add some attributes on to an object
whose definition I've never seen. So - unlikely, but I wouldn't rule
> Note that a value can have several different names (and unnamed references)
> pointing at it,
Ah - there's the rub !
All it takes (from my earlier example) is
fred = pattern
values += [ fred ]
Now which is "the real name" of values?
In my example I would have wanted "pattern", and now whatever ugly
hack we used to get at "the name" is turning into an complex module.
> so the above code is not very general, but it may illustrate
> some interesting points.
Thanks - it does indeed.
> My guess is that your entire approach may need rethinking. What is the
> ultimate objective of your project?
Find out what some other Pythonista have thought about this problem
OTOH: the actual objective was to remove the redundancy of using the
string "pattern" when I already have a perfectly good eponymous
variable. On balance I think allowing a little bit of redundancy is
prudent in this case :-)
More information about the Python-list