On 05/07/2015 09:50 PM, Alex Kleider wrote:
On 2015-04-21 16:48, Cameron Simpson wrote:
But it would not be schizophrenic to write a function that returned a
name arbitrarily, by inspecting locals(). It depends whether you only
need a name, or if you need "the" name.
Write yourself a "find_name_from_locals(local_map, value)" function
that reports. That will (a) get you a partial answer and (b) cement in
your mind what is possible and why. Easy and useful!
Cheers,
Cameron Simpson <c...@zip.com.au>
Well I finally got around to tackling this little challenge and you are
right:
It was illuminating.
For what it's worth, here's the code that made the light go on:
#!../venv/bin/python3
# -*- coding: utf-8 -*-
# vim: set file encoding=utf-8 :
#
# file: 'getname.py'
"""
def get_name(localmap, item):
"""As suggested.
Returns 'a' name, not necessarily 'the' name."""
for name in localmap:
if localmap[name] == item:
This is not likely to be what was intended. You want
if localmap[name] is item:
That can identify if one of the names happens to be bound to the SAME
object being examined. Rather than one that happens to have the same value.
--
DaveA
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor