[EMAIL PROTECTED] wrote: > Hi > > (snip) > >>>>def get_class_by_name(name): >>>> return globals()[name] >>> >>Q&D way to retrieve the class object (Python's classes are themselves >>objects) known by it's name (as a string). > > > ok, so it actually returns the class object itself. > One thing that confuses me is that objects have a name (self.name)
They dont - unless you give them one: >>> class Foo(object): pass ... >>> Foo.name Traceback (most recent call last): File "<stdin>", line 1, in ? AttributeError: type object 'Foo' has no attribute 'name' >>> f = Foo() >>> f.name Traceback (most recent call last): File "<stdin>", line 1, in ? AttributeError: 'Foo' object has no attribute 'name' >>> classes, functions and modules have a __name__ attribute, but that's something else... > but > object instances also have a name (e.g. myspecialturbine = turbine(...) Actually, this is the other way round: names refers ('are bound to') objects. Think of namespaces (the global or local one...) as dicts holding name:ref-to-object pairs (that's mostly what they are FWIW). Different names can refer to the same object. The object itself doesn't know anything about this. > ---- how do I discover the name 'myspecialturbine' ?). >From within the object refered by this name ? Short answer : you can't. > And object > instances have a class name too ('turbine'). Nope, they have a class, which itself as a __name__ attribute, which is the name that was used when "creating" the class object (ie: usually when the 'class' statement is eval'd at module load time). > Aaargh, too many names! > what if just want to know what the name of the instance is (in this > case 'myspecialturbine'?) you just named it so yourself, ie: myspecialturbine = SpecialTurbine() Else, no way. You can of course give a name when instanciating the object: class Named(object): def __init__(self, name): self.name = name n1 = Named('n1') print n1.name => n1 But this doesn't mean there's any correspondance between this name and a name in the current (or any other) namespace: toto = n1 n1 = "lalala" print toto.name => n1 locals()[toto.name] is toto => False -- bruno desthuilliers python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for p in '[EMAIL PROTECTED]'.split('@')])" -- http://mail.python.org/mailman/listinfo/python-list