can I query an instance for its name?

Kragen Sitaker kragen at dnaco.net
Sat Sep 30 22:20:55 EDT 2000


In article <9z6B5.941$8n3.59167 at carnaval.risq.qc.ca>,
Luc Lefebvre <lefebvre at med.mcgill.ca> wrote:
>I have been attempting to get access to instance names, eg:
>	x.who()	# would expect "x is an instance of class foo"

Instances don't have names in Python; only references to instances have
names, and they often have a lot of names.  If module y knows x as x,
module z may know x as y.x, and module w may know y only as z.y and
thus x as z.y.x.

And you can make more names for the same instance; if you say y=x, then
afterwards, y will be another name for the instance x.

Eventually, instances have no names, and then they are destroyed.

So, no.  Although you can do something like what I did in my "syntactic
sugar for filesystem access" post, and tell an object its name when
it's created.  Or you can rummage through a tree of objects for a
particular object, and then print the path by which it was found.

>Also, is there a way to enquire as to a given classes children eg:
>foo.__children__?

By "classes children" do you mean "class's subclasses"?  I don't think
there is.
-- 
<kragen at pobox.com>       Kragen Sitaker     <http://www.pobox.com/~kragen/>
Perilous to all of us are the devices of an art deeper than we ourselves
possess.
                -- Gandalf the Grey [J.R.R. Tolkien, "Lord of the Rings"]





More information about the Python-list mailing list