[Tutor] What's in a name?
Keith Winston
keithwins at gmail.com
Sat Jan 4 01:25:03 CET 2014
Thanks Walter, I think I've got the lay of the land roughly, but my grasp
is still light and imperfect. I'm pretty confident I shouldn't be doing
anything like what you're describing for the level of coding I'm doing, but
it's interesting to see the approach.
Keith
On Fri, Jan 3, 2014 at 6:56 PM, Walter Prins <wprins at gmail.com> wrote:
> Hi,
>
> The code in my last post got line wrapped which will break if directly
> tried out. To avoid possible confusion I paste a version below with
> shorter lines which should avoid the problem:
>
>
> import gc, itertools
>
> def names_of(obj):
> """Try to find the names associated to a given object."""
> #Find dict objects from the garbage collector:
> refs_dicts = (ref for ref in gc.get_referrers(obj)
> if isinstance(ref, dict))
> #Get a single chained generator for all the iteritems() iterators
> #in all the dicts
> keyvalue_pairs = itertools.chain.from_iterable(
> refd.iteritems() for refd in refs_dicts)
> #Return a list of keys (names) where the value (obj) is the one
> #we're looking for:
> return [k for k, v in keyvalue_pairs if v is obj]
>
> x = []
> y = x
> z = [x]
>
> print names_of(z[0])
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
--
Keith
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140103/82fe06a5/attachment-0001.html>
More information about the Tutor
mailing list