Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   How to refer to class name and function name in a python program? (http://www.velocityreviews.com/forums/t698861-how-to-refer-to-class-name-and-function-name-in-a-python-program.html)

Peng Yu 09-20-2009 03:38 PM

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

r 09-20-2009 04:17 PM

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?

Vijayendra Bapte 09-20-2009 04:32 PM

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


Peng Yu 09-20-2009 04:43 PM

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

r 09-20-2009 05:01 PM

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. ;-)


Benjamin Kaplan 09-20-2009 05:43 PM

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
>


alex23 09-21-2009 12:09 AM

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?


Peng Yu 09-29-2009 01:54 AM

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

Gabriel Genellina 10-06-2009 03:05 AM

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.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57