[Tutor] introspection

Cameron Simpson cs at zip.com.au
Wed Apr 22 01:48:03 CEST 2015


On 21Apr2015 09:59, Alex Kleider <akleider at sonic.net> wrote:
>On 2015-04-20 22:21, Danny Yoo wrote:
>>What's supposed to happen in this situation?
>>##########################################
>>class Person(object):
>>    def __init__(self): pass
>>
>>j = Person()
>>john = j
>>jack = j
>>##########################################
>>
>>What single name should we get back from the single Person object
>>here?  "j", "john", or "jack"?
>
>I was hoping that it would be possible to create a function
>that would do the following:
>
>def my_name(some_object):
>  return some_object.__name__
>
>so, using what you entered above..
>>>>my_name(j)
>'j'
>>>>my_name(john)
>'john'
>>>>my_name(jack)
>'jack'
>
>But I see what I think you and others have been trying to explain to 
>me: that the expression some_object.__name__, if it existed, would 
>indeed be schizophrenic since it would be an attribute of the object, 
>not the name(s) to which  it is bound.

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 <cs at zip.com.au>

Technique will get you through times of no strength a lot better than
strength will get you through times of no technique.    - the Nealomatic


More information about the Tutor mailing list