Printable string for 'self'

Don Taylor nospamformeSVP at gmail.com
Tue Mar 14 21:55:12 EST 2006


Michael Spencer wrote:
>
> 
> In general, this requires exhaustive search of name bindings e.g.,:
> 
>  >>> def get_names_of(obj, ns):
> ...     return [name for name, value in ns.iteritems() if value is obj]
> ...
>  >>> class A(object):
> ...     def global_names_bound_to_me(self):
> ...         return get_names_of(self, globals())
> ...
>  >>> a = A()
>  >>> a.global_names_bound_to_me()
> ['a']
>  >>> b = a
>  >>> a.global_names_bound_to_me()
> ['a', 'b']
>  >>>
> 
Ah, ok.

But, as you show in the example, this technique does not let me figure 
out which of 'a' or 'b' was used to qualify global_names_bound_to_me().

And if I call my method: A().qualify global_names_bound_to_me() then I 
get an empty list back.

Is what I want to do not possible without referring back to the source?

Don.





More information about the Python-list mailing list