name of object inside it's methods?

Fredrik Lundh effbot at telia.com
Sun Feb 27 19:14:37 CET 2000


Michal Wallace (sabren) wrote:
>
> > can someone fill in the blank
> >
> > -------------------------------
> > class Hello:
> >     def __init__(self, x):
> >         self.x = x
> >
> >     def classname(self):
> >         <blank>
>
> def classname(self):
>     return self.__class__
>
> it'll actually return "__main__.Hello", but you could just look for the
"."

given that one would expect a method called classname
to return the name of the class, and not a class object,
this probably works better:

    def classname(self):
        return self.__class__.__name__

...

on the other hand, was this really what "alv" asked for?

> > >>>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

which is (nearly) impossible.

</F>





More information about the Python-list mailing list