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

Discussion in 'Python' started by Peng Yu, Sep 20, 2009.

  1. Peng Yu

    Peng Yu Guest

    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
     
    Peng Yu, Sep 20, 2009
    #1
    1. Advertising

  2. Peng Yu

    r Guest

    On Sep 20, 10:38 am, Peng Yu <> 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?
     
    r, Sep 20, 2009
    #2
    1. Advertising

  3. On Sep 20, 8:38 pm, Peng Yu <> 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
     
    Vijayendra Bapte, Sep 20, 2009
    #3
  4. Peng Yu

    Peng Yu Guest

    On Sun, Sep 20, 2009 at 11:32 AM, Vijayendra Bapte
    <> wrote:
    > On Sep 20, 8:38 pm, Peng Yu <> 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
     
    Peng Yu, Sep 20, 2009
    #4
  5. Peng Yu

    r Guest

    On Sep 20, 10:38 am, Peng Yu <> 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. ;-)
     
    r, Sep 20, 2009
    #5
  6. 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 <> 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
    >
     
    Benjamin Kaplan, Sep 20, 2009
    #6
  7. Peng Yu

    alex23 Guest

    Peng Yu <> 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?
     
    alex23, Sep 21, 2009
    #7
  8. Peng Yu

    Peng Yu Guest

    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 <> 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
     
    Peng Yu, Sep 29, 2009
    #8
  9. 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/viewpost.jsp?thread=240808
    [3] http://pypi.python.org/pypi/decorator

    --
    Gabriel Genellina
     
    Gabriel Genellina, Oct 6, 2009
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tor Iver Wilhelmsen
    Replies:
    0
    Views:
    424
    Tor Iver Wilhelmsen
    Jul 18, 2003
  2. Jayaram
    Replies:
    0
    Views:
    446
    Jayaram
    Jul 18, 2003
  3. Replies:
    3
    Views:
    12,254
  4. Sullivan WxPyQtKinter
    Replies:
    8
    Views:
    284
    Kay Schluehr
    Mar 11, 2006
  5. john
    Replies:
    1
    Views:
    3,487
    Steven Cheng[MSFT]
    Jan 24, 2008
Loading...

Share This Page