name of object inside it's methods?
Fredrik Lundh
effbot at telia.com
Sun Feb 27 08:51:46 EST 2000
alv50855 at batman.tamucc.edu wrote:
> can someone fill in the blank
>
> -------------------------------
> class Hello:
> def __init__(self, x):
> self.x = x
>
> def classname(self):
> <blank>
>
> -------------------------------
>
> >>>from hello import *
> >>>clown = Hello(1)
> >>>clown.classname()
> clown
>
> In other words I want the method to print the name of the object that it
> belongs to
sorry, you can't do that.
names are just names, not identities. any
object can have lots of different names, or
none at all.
an example:
clown = Hello(1)
bobo = clown
# here, bobo and clown both point to
# the same instance
del clown
# now, only bobo points to that instance
mylist = [bobo]
del bobo
# and here, nobody points directly to the
# instance.
</F>
More information about the Python-list
mailing list