![]() |
How to refer to class name and function name in a python program?
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 |
Re: How to refer to class name and function name in a python program?
On Sep 20, 10:38*am, 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 well thats easy ;-) return 'A, __repr__' was that too easy? |
Re: How to refer to class name and function name in a python program?
On Sep 20, 8:38*pm, 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 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 |
Re: How to refer to class name and function name in a python program?
On Sun, Sep 20, 2009 at 11:32 AM, Vijayendra Bapte
<vijayendra.bapte@gmail.com> wrote: > On Sep 20, 8:38*pm, 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 > > 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 |
Re: How to refer to class name and function name in a python program?
On Sep 20, 10:38*am, Peng Yu <pengyu...@gmail.com> wrote:
> 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. ;-) |
Re: How to refer to class name and function name in a python program?
On Sun, Sep 20, 2009 at 12:43 PM, Peng Yu <pengyu.ut@gmail.com> wrote:
> On Sun, Sep 20, 2009 at 11:32 AM, Vijayendra Bapte > <vijayendra.bapte@gmail.com> wrote: >> On Sep 20, 8:38*pm, 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 >> >> 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__) > -- > http://mail.python.org/mailman/listinfo/python-list > |
Re: How to refer to class name and function name in a python program?
Peng Yu <pengyu...@gmail.com> wrote:
> 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? |
Re: How to refer to class name and function name in a python program?
On Sun, Sep 20, 2009 at 12:43 PM, Benjamin Kaplan
<benjamin.kaplan@case.edu> wrote: > On Sun, Sep 20, 2009 at 12:43 PM, Peng Yu <pengyu.ut@gmail.com> wrote: >> On Sun, Sep 20, 2009 at 11:32 AM, Vijayendra Bapte >> <vijayendra.bapte@gmail.com> wrote: >>> On Sep 20, 8:38*pm, 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 >>> >>> 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 |
Re: How to refer to class name and function name in a python program?
En Mon, 28 Sep 2009 22:54:55 -0300, Peng Yu <pengyu.ut@gmail.com> escribió:
> On Sun, Sep 20, 2009 at 12:43 PM, Benjamin Kaplan > <benjamin.kaplan@case.edu> wrote: >> On Sun, Sep 20, 2009 at 12:43 PM, Peng Yu <pengyu.ut@gmail.com> wrote: >>> On Sun, Sep 20, 2009 at 11:32 AM, Vijayendra Bapte >>> <vijayendra.bapte@gmail.com> wrote: >>>> class A: >>>> >>>> @echo >>>> def __repr__(self): >>>> pass >>> >>> What does @echo mean? >> >> It's a decorator, which wraps the function with another function > > 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/viewpo...?thread=240808 [3] http://pypi.python.org/pypi/decorator -- Gabriel Genellina |
| All times are GMT. The time now is 04:25 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.