Skip to content

Instantly share code, notes, and snippets.

@kevinw
Created December 16, 2010 19:06
Show Gist options
  • Save kevinw/743808 to your computer and use it in GitHub Desktop.
Save kevinw/743808 to your computer and use it in GitHub Desktop.
def nameof(obj):
'''
if obj is referenced strongly in another object's __dict__, give its attribute name.
'''
for r in gc.get_referrers(obj):
for r2 in gc.get_referrers(r):
if r is getattr(r2, '__dict__', object()):
for k, v in r.iteritems():
if v is obj:
return '[%r attribute of %r]' % (k, r2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment