How can I get the name of an object???
spex66 at my-deja.com
spex66 at my-deja.com
Fri Apr 28 11:20:25 EDT 2000
corrected code version if my cancel haven't worked:
In article <slrn8gglsv.ep7.scarblac-spamtrap at flits104-37.flits.rug.nl>,
scarblac-rt at pino.selwerd.nl wrote:
> spex66 at my-deja.com wrote in comp.lang.python:
> > In article <etd8zy03m44.fsf at w20-575-108.mit.edu>,
> > Alex <cut_me_out at hotmail.com> wrote:
> > >
> > > An object could be referred to by any number of names:
> > >
> > > dd = zz ()
> > > ee = dd
> > >
> > > Which name for the object do you wish to know? ee or dd?
> >
> > dd or in other words, the name of the FIRST made instance...
>
> What would happen in the following cases:
> 1)
> dd = zz()
> ee = dd
> del dd
> print ee.__name__
> 2)
> dd = [xx(), yy(), zz()]
> print dd[2].__name__
> 3)
> print zz().__name__
>
> Classes have a name (the one used in their class: statement).
> Instances have no name, period.
OK, I understand this point very well :-) there are enough good examples
> But why would you need it? In most cases where people try to use
something
I need it to build navigation-code on the fly for "exec" command. Build
on a knitted database API. I have an instance of the navigation-class,
but for the "exec"code I need the name of the *instance*.
Good for me: there is only one instance necessary, but I don't wanted
to hardcode the name, cause an interactive use i sto restricted.
So I have actually a new approach: a "shadow" instance, of the first
navigation instance.
#FILE: shadow.py
"""arghh, last 45min I implemented the __setattr__, I don't needed in
my example, but I think it's nice to play with
"""
class shadow:
#BUILD for internal access on the given instance with a reliable
name
#ONLY practible if there is ONE instance of a class
#Needed for internal code-building purposes
def hide(self, realinstance):
self.internal = realinstance
def __getattr__(self, attrName):
# Get any other interesting attributes from the base class.
if (attrName <> 'internal'):
return self.internal.__dict__[attrName]
else:
if self.__dict__.has_key('internal'):
return self.__dict__['internal']
def __setattr__(self, attrName, value):
if (attrName <> 'internal'):
setattr(self.internal, attrName, value)
else:
self.__dict__['internal'] = value
sw = shadow()
shadowName = 'sw'
class navigation:
def __init__(self):
self.name = 'hallo'
sw.hide(self)
#FILE:END
so I can write at the prompt:
>>> from shadow import * #IMPORTANT for me
>>> yy = navigation()
>>> yy.name
>>> 'hallo'
>>> sw.name
>>> 'hallo'
>>> sw.test = 7
>>> yy.test
>>> 5
and I have my fixed access for the navigation instance over shadowName
>>> exec "%s.name = '%s'" % (shadowName, 'hi')
>>> yy.name
>>> 'hi'
hmm, and it work for all classes with one instance (I hope :))
and a nice excercise for __getattr__ and __setattr__
greetings
Peter
(=PA=)
> like this, dictionaries are a solution.
>
> --
> Remco Gerlich
Sent via Deja.com http://www.deja.com/
Before you buy.
More information about the Python-list
mailing list