Who am I: can a class instance determine its own name?
Emile van Sebille
emile at fenx.com
Wed Mar 14 12:04:06 EST 2001
Has locals changed so that it may be written to reliably? My documentation
still warns:
locals ()
Return a dictionary representing the current local symbol table. Warning:
The contents of this dictionary should not be modified; changes may not
affect the values of local variables used by the interpreter.
Emile van Sebille
emile at fenx.com
"Harry George" <hgg9140 at cola.ca.boeing.com> wrote in message
news:xqxzoeol3ju.fsf at cola.ca.boeing.com...
> class MyName:
> #--------------------------
> def init(self,name):
> self.name=name
> try:
> raise None
> except:
> c=sys.exc_info()[2].tb_frame.f_back.f_back
> c.f_locals[name]=self
> ...
>
> MyName("me")
> print me.name
>
> "Kevin Cazabon" <kevin_cazabon at hotmail.nospamplease!.com> writes:
>
> > The reason I say it's not elegant is that there is the possibility (read
> > "probability") that the assigned name and the actual name can differ.
> >
> > We're assuming that the programmer assigns exactly the same name to
both,
> > which doesn't seem like a hard task, but it introduces the possibility
of
> > problems, and probably hard ones to debug too.
> >
> > If there were an automatic method, it would be MUCH more reliable.
> >
> > Just MHO.
> >
> > Kevin.
> >
> >
> >
> > "Paul Prescod" <paulp at ActiveState.com> wrote in message
> > news:mailman.984324974.25480.python-list at python.org...
> > | Kevin Cazabon wrote:
> > | >
> > | > ...
> > | > You could also
> > | > make it an initialization parameter.
> > | >
> > | > FooBar = Foo(name="FooBar")
> > | >
> > | > Just trying to help... not elegantly though.
> > |
> > | I disagree Kevin. This is not only the canonical way to give an
instance
> > | a name, it is also the elegant way. It gives you a lot more
flexibility
> > | because the name it has at runtime is unrelated to the variable name
you
> > | happened to have given it in your code. If you need to change one or
the
> > | other for any reason, you can do so safely.
> > |
> > | Paul Prescod
> > |
> >
> >
>
> --
> Harry George E-mail: harry.g.george at boeing.com
> The Boeing Company Renton: (425) 237-6915
> P. O. Box 3707 02-CA Everett: (425) 266-3868
> Seattle, WA 98124-2207 Page: (425) 631-8803
More information about the Python-list
mailing list