Peng Yu <pengyu...@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.
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to