Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > __doc__ of current function?

Reply
Thread Tools

__doc__ of current function?

 
 
Trevor Taylor
Guest
Posts: n/a
 
      09-02-2004
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
 
Reply With Quote
 
 
 
 
Ksenia Marasanova
Guest
Posts: n/a
 
      09-02-2004
>
> ... 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.

 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      09-02-2004
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

 
Reply With Quote
 
Sean Ross
Guest
Posts: n/a
 
      09-02-2004

"Peter Otten" <(E-Mail Removed)> wrote in message
news:ch6uj8$t68$00$(E-Mail Removed)-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'
>>>



 
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
the address of list.append and list.append.__doc__ HYRY Python 10 09-26-2007 09:41 AM
Minor bug in tempfile module (possibly __doc__ error) James T. Dennis Python 9 05-11-2007 07:25 AM
Printing __doc__ gtb Python 7 03-22-2007 04:36 PM
__doc__ in compiled script Gabriel Genellina Python 5 11-03-2006 09:14 AM
property __doc__ David Isaac Python 1 07-01-2006 03:48 AM



Advertisments