How to refer to class name and function name in a python program?

Peng Yu pengyu.ut at gmail.com
Mon Sep 28 21:54:55 EDT 2009


On Sun, Sep 20, 2009 at 12:43 PM, Benjamin Kaplan
<benjamin.kaplan at case.edu> wrote:
> On Sun, Sep 20, 2009 at 12:43 PM, Peng Yu <pengyu.ut at gmail.com> wrote:
>> On Sun, Sep 20, 2009 at 11:32 AM, Vijayendra Bapte
>> <vijayendra.bapte at gmail.com> wrote:
>>> On Sep 20, 8:38 pm, Peng Yu <pengyu... 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
>>>
>>> Using decorator:
>>> ----------------
>>>
>>> def echo(func):
>>>    def _echo(self, *args, **kw):
>>>        return "In %s.%s" % (self.__class__.__name__, func.func_name)
>>>
>>>    return _echo
>>>
>>> class A:
>>>
>>>    @echo
>>>    def __repr__(self):
>>>        pass
>>>
>>> a = A()
>>> print a
>>
>> What does @echo mean?
>>
>> Regards,
>> Peng
>
> It's a decorator, which wraps the function with another function it's
> the equivalent of calling
>
> def __repr__(self) :
>    pass
>
> __repr__ = echo(__repr__)

I looked at the table of content of python tutorial at
http://docs.python.org/tutorial/

But I don't see which section discuss this concept. If it is there,
would you please let me know which section I should read. Or this
concept is discussed somewhere else?

Regards,
Peng



More information about the Python-list mailing list