Could someone please give an example?

Joshua Rosen rozzin at geekspace.com
Sat Jun 26 01:27:19 CEST 1999


Emile van Sebille wrote:
> 
> I'm new at this, but it seems I've read that the tracestack is
> available.  Might it be possible to read in the calling line number,
> then parse the source code for your 'name'?

Sure, but what'd be the point?
Let's say that you define a function to tell you the name:

def tn(inst):
    ....

Now, if you can call that function, with an instance as an argument, then you
already know what `name' you are calling it by.

If you've got an instanced produced and `stored' by something like...

class C:
  ...

i = C()

... then you need to call `tn(i)'--there you go, "i" is the name of the class,
eh?

If you want the program to decide which variable to look at, then you need to
use something like `eval(XXX)', where `XXX' is an expression that returns a
string, so, if you want to know the `name' of the variable, there, then you can
just turn to whatever `XXX' returned.

I've yet to see a reason why this `finding the name of an instance' is needed,
or what it accomplishes. Just by virtue of `the only name that an instance has
is what you give it', there really /shouldn't/ be a problem--if you give it a
name, you should be able to `remember' what the name is, or at least where you
stored it.

		Would someone please give an example of the use of this, assuming that we
have a function that does it (say..., `tn(xxx)')?

			-Rozzin.





More information about the Python-list mailing list