Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #9172 > unrolled thread

Function docstring as a local variable

Started byTim Johnson <tim@johnsons-web.com>
First post2011-07-10 09:41 -0800
Last post2011-07-10 21:05 -0700
Articles 7 — 7 participants

Back to article view | Back to comp.lang.python


Contents

  Function docstring as a local variable Tim Johnson <tim@johnsons-web.com> - 2011-07-10 09:41 -0800
    Re: Function docstring as a local variable rantingrick <rantingrick@gmail.com> - 2011-07-10 10:44 -0700
      Re: Function docstring as a local variable "Colin J. Williams" <cjw@ncf.ca> - 2011-07-10 18:28 -0400
        Re: Function docstring as a local variable Ben Finney <ben+python@benfinney.id.au> - 2011-07-11 09:48 +1000
      Re: Function docstring as a local variable Corey Richardson <kb1pkl@aim.com> - 2011-07-10 19:00 -0400
        Re: Function docstring as a local variable Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-07-11 11:54 +1000
        Re: Function docstring as a local variable alex23 <wuwei23@gmail.com> - 2011-07-10 21:05 -0700

#9172 — Function docstring as a local variable

FromTim Johnson <tim@johnsons-web.com>
Date2011-07-10 09:41 -0800
SubjectFunction docstring as a local variable
Message-ID<mailman.842.1310319682.1164.python-list@python.org>
Consider the following:
## code
def test():
    """This is my docstring"""
    print(??)  ## can I print the docstring above?
## /code
It possible for a function to print it's own docstring?

thanks
(pointers to docs could be sufficient)
-- 
Tim 
tim at johnsons-web dot com or akwebsoft dot com
http://www.akwebsoft.com

[toc] | [next] | [standalone]


#9173

Fromrantingrick <rantingrick@gmail.com>
Date2011-07-10 10:44 -0700
Message-ID<64b5198e-5cc7-4790-898e-7f1abb199230@y24g2000yqb.googlegroups.com>
In reply to#9172
On Jul 10, 12:41 pm, Tim Johnson <t...@johnsons-web.com> wrote:
> It possible for a function to print it's own docstring?

def f():
   """docstring"""
   print "docstring"

any questions?

[toc] | [prev] | [next] | [standalone]


#9189

From"Colin J. Williams" <cjw@ncf.ca>
Date2011-07-10 18:28 -0400
Message-ID<mailman.855.1310336914.1164.python-list@python.org>
In reply to#9173
On 10-Jul-11 13:44 PM, rantingrick wrote:
> On Jul 10, 12:41 pm, Tim Johnson<t...@johnsons-web.com>  wrote:
>> It possible for a function to print it's own docstring?
>
> def f():
>     """docstring"""
>     print "docstring"
>
> any questions?

Try:

def f():
      ds= """docstring"""
      print ds
 >

Colin W.

[toc] | [prev] | [next] | [standalone]


#9204

FromBen Finney <ben+python@benfinney.id.au>
Date2011-07-11 09:48 +1000
Message-ID<87d3hhg0ip.fsf@benfinney.id.au>
In reply to#9189
"Colin J. Williams" <cjw@ncf.ca> writes:

> On 10-Jul-11 13:44 PM, rantingrick wrote:
> > On Jul 10, 12:41 pm, Tim Johnson<t...@johnsons-web.com>  wrote:
> >> It possible for a function to print it's own docstring?
> >
> > def f():
> >     """docstring"""
> >     print "docstring"
>
> Try:
>
> def f():
>      ds= """docstring"""
>      print ds

The OP wants the function to print its own docstring, which your example
does not do. You've defined a function with an empty docstring.

    >>> def foo():
    ...     ds = "The Larch"
    ...     print ds
    ... 
    >>> foo.__doc__
    >>>

-- 
 \       “Firmness in decision is often merely a form of stupidity. It |
  `\        indicates an inability to think the same thing out twice.” |
_o__)                                                —Henry L. Mencken |
Ben Finney

[toc] | [prev] | [next] | [standalone]


#9196

FromCorey Richardson <kb1pkl@aim.com>
Date2011-07-10 19:00 -0400
Message-ID<mailman.858.1310339158.1164.python-list@python.org>
In reply to#9173

[Multipart message — attachments visible in raw view] — view raw

Excerpts from Colin J. Williams's message of Sun Jul 10 18:28:15 -0400 2011:
> Try:
> 
> def f():
>       ds= """docstring"""
>       print ds

That doesn't actually make a docstring, though. It makes a string object and
points the name ds at it. Do you know what a docstring is?

def foo():
    """I am a docstring"""
    pass

def bar():
    ds = "I am not a docstring!"

def baz():
    "I am a docstring too!"
    pass

def qux():
    'And even me! Quote type don't matter (besides style)'
     pass
-- 
Corey Richardson
  "Those who deny freedom to others, deserve it not for themselves"
     -- Abraham Lincoln

[toc] | [prev] | [next] | [standalone]


#9214

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2011-07-11 11:54 +1000
Message-ID<4e1a57df$0$29987$c3e8da3$5496439d@news.astraweb.com>
In reply to#9196
On Mon, 11 Jul 2011 09:00 am Corey Richardson wrote:

> Excerpts from Colin J. Williams's message of Sun Jul 10 18:28:15 -0400
> 2011:
>> Try:
>> 
>> def f():
>>       ds= """docstring"""
>>       print ds
> 
> That doesn't actually make a docstring, though. It makes a string object
> and points the name ds at it. Do you know what a docstring is?


Colin might not be aware of why docstrings are useful, and arbitrary local
variables ds="""docstring""" are not.


def f1():
    """This is my documentation string.

    Imagine this has actual useful information.
    """

def f2():
    dc = """This is my documentation string.

    Imagine this has actual useful information.
    """


Now, at the interactive interpreter, call:

help(f1)
help(f2)



-- 
Steven

[toc] | [prev] | [next] | [standalone]


#9219

Fromalex23 <wuwei23@gmail.com>
Date2011-07-10 21:05 -0700
Message-ID<f6ccf302-ff76-4cf2-b02e-21a9147a55ad@h7g2000prf.googlegroups.com>
In reply to#9196
On Jul 11, 9:00 am, Corey Richardson <kb1...@aim.com> wrote:
> def qux():
>     'And even me! Quote type don't matter (besides style)'

Well, style and the presence of the string literal notation in the
quoted text :)

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web