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

P

Peng Yu

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
 
R

r

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

well thats easy ;-)

return 'A, __repr__'

was that too easy?
 
V

Vijayendra Bapte

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
 
P

Peng Yu

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
 
R

r

Hi,

I have the following code. I want to change the function body of
__repr__ to something like


PS: Methods get angry when you refer to them as functions . You see,
methods feel that they are more than a mere lowly function, and have
developed a superiority complex about it. And you would not want to
get them upset either -- since they have friends in high places
(classes) and can really put a hurt on you. ;-)
 
B

Benjamin Kaplan

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__)
 
A

alex23

Peng Yu said:
What does @echo mean?

Have you read _any_ of the documentation? Or is this all an exercise
in seeing if you can convince a group of disparate strangers to teach
you Python for free?
 
P

Peng Yu

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
 
G

Gabriel Genellina

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?

It isn't menctioned in the tutorial - look in the Glossary, or the
original PEP [1]
There's a good introduction by Bruce Eckel at [2], and the decorator
module by M. Simionato is a must [3]

[1] http://www.python.org/dev/peps/pep-0318/
[2] http://www.artima.com/weblogs/viewpost.jsp?thread=240808
[3] http://pypi.python.org/pypi/decorator
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top