__doc__ of current function?

Discussion in 'Python' started by Trevor Taylor, Sep 2, 2004.

  1. Hi,

    I thought it would be simple but couldn't figure it out, nor find it
    addressed already:

    I can do:

    def validatePassword(p):
    'do something'
    print validatePassword.__doc__

    .... can I write a general docOfCurrentFunction() function so that I
    can rewrite it:

    def validatePassword(p):
    'do something'
    print docOfCurrentFunction()

    Thanks,

    Trevor
    Trevor Taylor, Sep 2, 2004
    #1
    1. Advertising

  2. >
    > ... can I write a general docOfCurrentFunction() function so that I
    > can rewrite it:
    >
    > def validatePassword(p):
    > 'do something'
    > print docOfCurrentFunction()
    >


    def docOfCurrentFunction():
    import sys
    func_name = sys._getframe(1).f_code.co_name
    return eval(func_name + '.__doc__')

    But I hope that a more intelligent way exists than using 'eval' :)


    Ksenia.
    Ksenia Marasanova, Sep 2, 2004
    #2
    1. Advertising

  3. Trevor Taylor

    Peter Otten Guest

    Ksenia Marasanova wrote:

    >>
    >> ... can I write a general docOfCurrentFunction() function so that I
    >> can rewrite it:
    >>
    >> def validatePassword(p):
    >> 'do something'
    >> print docOfCurrentFunction()
    >>

    >
    > def docOfCurrentFunction():
    > import sys
    > func_name = sys._getframe(1).f_code.co_name
    > return eval(func_name + '.__doc__')
    >
    > But I hope that a more intelligent way exists than using 'eval' :)


    Maybe

    >>> import sys
    >>> def caller():

    .... f = sys._getframe(1)
    .... return f.f_globals[f.f_code.co_name]
    ....
    >>> def demo():

    .... "Demo docstring"
    .... print caller().__doc__
    ....
    >>> demo()

    Demo docstring
    >>>


    Or can that fail in some cases?

    Peter
    Peter Otten, Sep 2, 2004
    #3
  4. Trevor Taylor

    Sean Ross Guest

    "Peter Otten" <> wrote in message
    news:ch6uj8$t68$00$-online.com...
    > Ksenia Marasanova wrote:
    >
    > >>
    > >> ... can I write a general docOfCurrentFunction() function so that I
    > >> can rewrite it:
    > >>
    > >> def validatePassword(p):
    > >> 'do something'
    > >> print docOfCurrentFunction()
    > >>

    > >
    > > def docOfCurrentFunction():
    > > import sys
    > > func_name = sys._getframe(1).f_code.co_name
    > > return eval(func_name + '.__doc__')
    > >
    > > But I hope that a more intelligent way exists than using 'eval' :)

    >
    > Maybe
    >
    > >>> import sys
    > >>> def caller():

    > ... f = sys._getframe(1)
    > ... return f.f_globals[f.f_code.co_name]
    > ...
    > >>> def demo():

    > ... "Demo docstring"
    > ... print caller().__doc__
    > ...
    > >>> demo()

    > Demo docstring
    > >>>

    >
    > Or can that fail in some cases?
    >
    > Peter
    >



    Hi.
    Yes, it fails for methods:

    >>> import sys
    >>> def caller():

    .... f = sys._getframe(1)
    .... return f.f_globals[f.f_code.co_name]
    ....
    >>> class C:

    .... def m(self):
    .... "mmmmmmmmmm"
    .... print caller().__doc__
    ....
    >>> c = C()
    >>> c.m()

    Traceback (most recent call last):
    File "<interactive input>", line 1, in ?
    File "<interactive input>", line 4, in m
    File "<interactive input>", line 3, in caller
    KeyError: 'm'
    >>>
    Sean Ross, Sep 2, 2004
    #4
    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. David Isaac

    property __doc__

    David Isaac, Jul 1, 2006, in forum: Python
    Replies:
    1
    Views:
    334
    Robert Kern
    Jul 1, 2006
  2. Gabriel Genellina

    __doc__ in compiled script

    Gabriel Genellina, Nov 2, 2006, in forum: Python
    Replies:
    5
    Views:
    269
    Peter Otten
    Nov 3, 2006
  3. gtb

    Printing __doc__

    gtb, Mar 21, 2007, in forum: Python
    Replies:
    7
    Views:
    288
    Bart Ogryczak
    Mar 22, 2007
  4. James T. Dennis
    Replies:
    9
    Views:
    314
    Dennis Lee Bieber
    May 11, 2007
  5. SanPy
    Replies:
    6
    Views:
    827
    Gabriel Genellina
    Oct 3, 2007
Loading...

Share This Page