How to refer to class name and function name in a python program?
Duncan Booth
duncan.booth at invalid.invalid
Sun Sep 20 12:29:40 EDT 2009
Peng Yu <pengyu.ut at gmail.com> wrote:
> Hi,
>
> I have the following code. I want to change the function body of
> __repr__ to something like
>
> return 'In %s::%s' % ($class_name, $function_name)
>
> I'm wondering what I should write for $class_name and $function_name
> in python.
>
> Regards,
> Peng
>
> class A:
> def __init__(self):
> pass
>
> def __repr__(self):
> return 'In A::__repr__'
>
> a = A()
> print a
>
def __repr__(self):
return 'In %s::__repr__' % self.__class__.__name__
You already know the function is called __repr__ so you don't need to look
it up anywhere. If you really wanted to do it the hard way:
import inspect
...
def __repr__(self):
return 'In %s::%s' % (self.__class__.__name__,
inspect.getframeinfo(inspect.currentframe()).function)
but just repeating __repr__ explicitly is less typing.
More information about the Python-list
mailing list