Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How to refer to class name and function name in a python program?

Reply
Thread Tools

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

 
 
Peng Yu
Guest
Posts: n/a
 
      09-20-2009
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
 
Reply With Quote
 
 
 
 
r
Guest
Posts: n/a
 
      09-20-2009
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?
 
Reply With Quote
 
 
 
 
Vijayendra Bapte
Guest
Posts: n/a
 
      09-20-2009
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

 
Reply With Quote
 
Peng Yu
Guest
Posts: n/a
 
      09-20-2009
On Sun, Sep 20, 2009 at 11:32 AM, Vijayendra Bapte
<> 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
 
Reply With Quote
 
r
Guest
Posts: n/a
 
      09-20-2009
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.

 
Reply With Quote
 
Benjamin Kaplan
Guest
Posts: n/a
 
      09-20-2009
On Sun, Sep 20, 2009 at 12:43 PM, Peng Yu <> wrote:
> On Sun, Sep 20, 2009 at 11:32 AM, Vijayendra Bapte
> <> 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
>

 
Reply With Quote
 
alex23
Guest
Posts: n/a
 
      09-21-2009
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?

 
Reply With Quote
 
Peng Yu
Guest
Posts: n/a
 
      09-29-2009
On Sun, Sep 20, 2009 at 12:43 PM, Benjamin Kaplan
<> wrote:
> On Sun, Sep 20, 2009 at 12:43 PM, Peng Yu <> wrote:
>> On Sun, Sep 20, 2009 at 11:32 AM, Vijayendra Bapte
>> <> 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
 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      10-06-2009
En Mon, 28 Sep 2009 22:54:55 -0300, Peng Yu <> escribió:
> On Sun, Sep 20, 2009 at 12:43 PM, Benjamin Kaplan
> <> wrote:
>> On Sun, Sep 20, 2009 at 12:43 PM, Peng Yu <> wrote:
>>> On Sun, Sep 20, 2009 at 11:32 AM, Vijayendra Bapte
>>> <> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How to refer to the function object itself in the function per se? Sullivan WxPyQtKinter Python 8 03-11-2006 11:05 PM
Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class. DJ Dev ASP .Net 3 02-08-2004 04:19 PM
Re: How does an inner class (non-static) refer to members of it's enclosing class? Jayaram Java 0 07-18-2003 05:03 PM
Re: How does an inner class (non-static) refer to members of it's enclosing class? Tor Iver Wilhelmsen Java 0 07-18-2003 08:41 AM



Advertisments