Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
How to comment constant values?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  8 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Erwin Mueller  
View profile  
 More options Jul 26 2009, 5:49 pm
Newsgroups: comp.lang.python
From: Erwin Mueller <dev...@deventm.org>
Date: Mon, 27 Jul 2009 07:49:58 +1000
Local: Sun, Jul 26 2009 5:49 pm
Subject: How to comment constant values?
Hello, I'm new to Python (using it for two months) and I wonder how can I
comment the const. values with the doc-strings. I.e. if I have code:

>FRACTION_MIN = 1
>FRACTION_MAX = 10

>class Fraction(collections.MutableSequence):
>    '''Model a fraction with denominators.

>    It contains one ore more denomintors in a list for quick access.
>    The class is thread save, it uses a lock for all operations.
>    '''

>    def __init__(self):
>            # ...

The class and the methods can have doc-strings, but what about the const.
values, FRACTION_MIN and FRACTION_MAX?

Thank you, Erwin.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Rebert  
View profile  
 More options Jul 26 2009, 6:06 pm
Newsgroups: comp.lang.python
From: Chris Rebert <c...@rebertia.com>
Date: Sun, 26 Jul 2009 15:06:29 -0700
Local: Sun, Jul 26 2009 6:06 pm
Subject: Re: How to comment constant values?

Only modules, classes, and functions/methods can have docstrings
associated with them.
For anything else, you have to use comments; or you can mention them
in the docstrings of related things.

Cheers,
Chris
--
http://blog.rebertia.com


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bearophile  
View profile  
 More options Jul 26 2009, 6:42 pm
Newsgroups: comp.lang.python
From: Bearophile <bearophileH...@lycos.com>
Date: Sun, 26 Jul 2009 15:42:22 -0700 (PDT)
Local: Sun, Jul 26 2009 6:42 pm
Subject: Re: How to comment constant values?
Chris Rebert:

> Only modules, classes, and functions/methods can have docstrings
> associated with them.
> For anything else, you have to use comments; or you can mention them
> in the docstrings of related things.

What about adding docstrings to other Python things, especially to
variables?

Bye,
bearophile


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Diez B. Roggisch  
View profile  
 More options Jul 26 2009, 6:47 pm
Newsgroups: comp.lang.python
From: "Diez B. Roggisch" <de...@nospam.web.de>
Date: Mon, 27 Jul 2009 00:47:08 +0200
Local: Sun, Jul 26 2009 6:47 pm
Subject: Re: How to comment constant values?
Chris Rebert schrieb:

While this is technically true, writing docstrings to constants (module
or classlevel) works when one uses tools such as epydoc to generate
documentation.

Diez


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Steven D'Aprano  
View profile  
 More options Jul 26 2009, 7:20 pm
Newsgroups: comp.lang.python
From: Steven D'Aprano <st...@REMOVE-THIS-cybersource.com.au>
Date: 26 Jul 2009 23:20:58 GMT
Local: Sun, Jul 26 2009 7:20 pm
Subject: Re: How to comment constant values?

On Sun, 26 Jul 2009 15:42:22 -0700, Bearophile wrote:
> Chris Rebert:
>> Only modules, classes, and functions/methods can have docstrings
>> associated with them.
>> For anything else, you have to use comments; or you can mention them in
>> the docstrings of related things.

> What about adding docstrings to other Python things, especially to
> variables?

Python doesn't have variables, it has objects bound to names. Because
__doc__ is looked up on the class, not the instance, there's not much
point in adding a docstring to the instance (even for instances which
allow attributes to be added). And because names are internally stored as
strings, you can't add a docstring to the name. So what you ask for is
impossible.

But of course that could be changed, if there was consensus that what you
ask for would be useful. But consider the consequences:

# A thought-experiment

>>> margin = 3  # allow a little bit of whitespace
>>> margin.__doc__ = "Extra space in pixels."
>>> help(x)

=> displays "Extra space in pixels."

So far so good. But now consider two equally unacceptable situations:

(1) If the docstring sticks to the object, then you get the docstring
inappropriately showing up in places where it should:

>>> number_of_widgets = len( [a, b, c] )
>>> help(number_of_widgets)

=> displays "Extra space in pixels."

(2) If the docstring sticks to the name, then you get the docstring
hanging around when you recycle the name to mean something else:

>>> margin = 15/100.0  # our profit margin is 15%
>>> help(margin)

=> displays "Extra space in pixels."

Now, in a large program, one shouldn't recycle names like that, but in a
large program, you're unlikely to programmatically look up docstrings.
Docstrings, and the help() function, are particularly useful in an
interactive session, which is precisely the time that you're likely to
recycle names.

In other words, it's currently impossible to bind docstrings to
"variables" in Python, but even more fundamentally, the request is
semantically incompatible with the Python name/object binding model.

--
Steven


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Steven D'Aprano  
View profile  
 More options Jul 26 2009, 7:23 pm
Newsgroups: comp.lang.python
From: Steven D'Aprano <st...@REMOVE-THIS-cybersource.com.au>
Date: 26 Jul 2009 23:23:30 GMT
Local: Sun, Jul 26 2009 7:23 pm
Subject: Re: How to comment constant values?

On Mon, 27 Jul 2009 00:47:08 +0200, Diez B. Roggisch wrote:
>> Only modules, classes, and functions/methods can have docstrings
>> associated with them.
>> For anything else, you have to use comments; or you can mention them in
>> the docstrings of related things.

> While this is technically true, writing docstrings to constants (module
> or classlevel) works when one uses tools such as epydoc to generate
> documentation.

I've never used epydoc, so I'm not sure what you mean. Presumably it uses
source code analysis to detect:

CONSTANT = 42
"""This is a constant."""

even though the string is ignored by the compiler.

Is that correct?

--
Steven


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Kern  
View profile  
 More options Jul 27 2009, 12:16 am
Newsgroups: comp.lang.python
From: Robert Kern <robert.k...@gmail.com>
Date: Sun, 26 Jul 2009 23:16:24 -0500
Local: Mon, Jul 27 2009 12:16 am
Subject: Re: How to comment constant values?
On 2009-07-26 18:23, Steven D'Aprano wrote:

epydoc 3 can actually parse the file to grab comments with a "#:" marker:

#: This is a constant.
CONSTANT = 42

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jean-Michel Pichavant  
View profile  
 More options Jul 27 2009, 10:17 am
Newsgroups: comp.lang.python
From: Jean-Michel Pichavant <jeanmic...@sequans.com>
Date: Mon, 27 Jul 2009 16:17:13 +0200
Local: Mon, Jul 27 2009 10:17 am
Subject: Re: How to comment constant values?

Yes, and because it is perfectly ignored by the compiler there's no harm
using this feature. I would add that even if you're not using epydoc,
having a way top discriminate comments from documentation is
recommended, their purpose are definitely not the same.

JM


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »